-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path131.cpp
63 lines (61 loc) · 1.81 KB
/
131.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// dp-dfs.cpp
class Solution {
void dfs(int index, string& s, vector<string>& path,
vector<vector<string>>& ret, vector<vector<bool>>& dp) {
if (index == s.size()) {
ret.push_back(path);
return;
}
for (int i = index; i < s.size(); ++i)
if (dp[index][i]) {
path.push_back(s.substr(index, i - index + 1));
dfs(i + 1, s, path, ret, dp);
path.pop_back();
}
}
public:
vector<vector<string>> partition(string s) {
int n = s.size();
if (n == 0)
return {};
vector<vector<string>> ret;
vector<string> path;
vector<vector<bool>> dp(n, vector<bool>(n, 0));
for (int i = 0; i < n; ++i)
for (int j = 0; j <= i; ++j)
dp[j][i] = s[i] == s[j] && (i - j <= 2 || dp[j + 1][i - 1]);
dfs(0, s, path, ret, dp);
return ret;
}
};
// dfs.cpp
class Solution2 {
void dfs(int index, string& s, vector<string>& path,
vector<vector<string>>& ret) {
if (index == s.size()) {
ret.push_back(path);
return;
}
for (int i = index; i < s.size(); ++i)
if (isPalindrome(s, index, i)) {
path.push_back(s.substr(index, i - index + 1));
dfs(i + 1, s, path, ret);
path.pop_back();
}
}
bool isPalindrome(const string& s, int start, int end) {
while (start < end)
if (s[start++] != s[end--])
return false;
return true;
}
public:
vector<vector<string>> partition(string s) {
vector<vector<string>> ret;
if (s.empty())
return ret;
vector<string> path;
dfs(0, s, path, ret);
return ret;
}
};