-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem131.cpp
42 lines (40 loc) · 1.19 KB
/
problem131.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
class Solution {
public:
vector<vector<string>>res;
int n;
vector<string>path;
string in;
bool checkPil(string str){
int l = 0, r = str.size()-1;
while(l < r){
if(str[l] != str[r]){
return false;
}
l++,r--;
}
return true;
}
vector<vector<string>> partition(string s) {
n = s.size();
in = s;
backtrack(0);
return res;
}
void backtrack(int i){
if(i == n){
res.push_back(path);
return;
}
for(int j = i+1;j <= n;j++){
string cut = in.substr(i, j - i);
if(checkPil(cut)){
// peek
path.push_back(cut);
// Recur to find other partitions
backtrack(j);
// leave
path.pop_back();
}
}
}
};