-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path131.cpp
More file actions
38 lines (35 loc) · 867 Bytes
/
Copy path131.cpp
File metadata and controls
38 lines (35 loc) · 867 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
34
35
36
37
38
class Solution {
public:
vector<vector<string>> partition(string s) {
int n = s.size();
if (n == 0)
return ret;
vector<string> temp;
BackTracking(s,n,0,temp);
return ret;
}
void BackTracking(string s,int &n,int i,vector<string> &temp){
if(i == n) {
ret.push_back(temp);
return;
}
for(int j =i ; j < n; ++j) {
if(isPalindrome(i,j,s)) {
temp.push_back(s.substr(i,j-i + 1));
BackTracking(s,n,j+1,temp);
temp.pop_back();
}
}
}
bool isPalindrome(int i, int j,string &s) {
while(i <= j &&s[i] == s[j]) {
++i;
--j;
}
if(i > j)
return true;
return false;
}
private:
vector<vector<string>> ret;
};