-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathBackTracking.cpp
42 lines (36 loc) · 954 Bytes
/
BackTracking.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
//leetcode palindrome partitioning
class Solution {
public:
bool ispalindrome(string s, int i, int j)
{
while(i<=j)
{
if(s[i++]!=s[j--])
return false;
}
return true;
}
void palindromepartition(vector<vector<string>>&res, vector<string>&path,string s, int index)
{
if(index>=s.length())
{
res.push_back(path);
return;
}
for(int i=index;i<s.length();i++)
{
if(ispalindrome(s,index,i))
{
path.push_back(s.substr(index,i-index+1));
palindromepartition(res,path,s,i+1);
path.pop_back();
}
}
}
vector<vector<string>> partition(string s) {
vector<vector<string>>res;
vector<string>path;
palindromepartition(res,path,s,0);
return res;
}
};