forked from elixered/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path131-palindrome-partitioning.cpp
47 lines (45 loc) · 1.12 KB
/
131-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:
vector<vector<string>> ans;
vector<vector<bool>> dp;
void solve(string& s, int idx, vector<string>& temp)
{
if(idx>=s.size())
{
ans.push_back(temp);
return;
}
int n = s.size();
for(int i=idx; i<n; i++)
{
if(dp[idx][i]==true)
{
temp.push_back(s.substr(idx,i-idx+1));
solve(s,i+1,temp);
temp.pop_back();
}
}
}
vector<vector<string>> partition(string s) {
int n = s.size();
dp = vector<vector<bool>>(n,vector<bool>(n,false));
for(int i=0; i<n; i++)
{
for(int c=i,r=0; c<n; c++,r++)
{
if(r==c)
dp[r][c] = true;
else
if(c==r+1)
dp[r][c] = (s[r]==s[c]);
else
{
dp[r][c] = (dp[r+1][c-1] && s[r]==s[c]);
}
}
}
vector<string> temp;
solve(s,0,temp);
return ans;
}
};