-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindromePartition.cpp
More file actions
33 lines (32 loc) · 882 Bytes
/
Copy pathPalindromePartition.cpp
File metadata and controls
33 lines (32 loc) · 882 Bytes
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
bool p[2048][2048]; // p[i][j] indicates whether s[i...j] is palindrome
class Solution {
vector<vector<string> > r;
public:
vector<vector<string> > partition(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int len = s.size();
for (int i = len - 1; i >= 0; i--) {
for (int j = i; j < len; j++) {
p[i][j] = s[i] == s[j] && (i + 1 >= j - 1 || p[i + 1][j - 1]);
}
}
r.clear();
vector<string> row;
partition(s, row, 0);
return r;
}
void partition(string s, vector<string> &row, int start) {
if (start == s.size()) {
r.push_back(row);
return;
}
for (int end = start; end < s.size(); end++) {
if (p[start][end]) {
row.push_back(s.substr(start, end - start + 1));
partition(s, row, end + 1);
row.pop_back();
}
}
}
};