-
Notifications
You must be signed in to change notification settings - Fork 0
/
Palindrome Partitioning.cpp
47 lines (45 loc) · 1.05 KB
/
Palindrome Partitioning.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
class Solution {
public:
void partition(string s, int b, int n, vector<int>& ret, vector<vector<bool> >& m,vector<vector<string> >& res) {
if(b==n)
{
vector<string> vs;
int k=0;
for(int i=0;i<ret.size();i++)
{
vs.push_back(s.substr(k,ret[i]-k+1));
k=ret[i]+1;
}
res.push_back(vs);
}
for(int i=b; i<n; i++)
{
if(m[i-b+1][b])
{
ret.push_back(i);
partition(s,i+1,n,ret, m ,res);
ret.pop_back();
}
}
}
vector<vector<string>> partition(string s) {
int n=s.length();
vector<vector<bool> > m(n+1, vector<bool>(n,false));
for(int len=1;len<=n;len++)
{
for(int i=0;i<=n-len;i++)
{
if(len==1)m[len][i]=true;
else if(len==2)m[len][i]=s[i]==s[i+1];
else
{
m[len][i]=m[len-2][i+1]&&s[i]==s[i+len-1];
}
}
}
vector<vector<string>> res;
vector<int> ret;
partition(s,0,n,ret,m,res);
return res;
}
};