-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindromePartitioning.cpp
More file actions
53 lines (48 loc) · 1.38 KB
/
Copy pathPalindromePartitioning.cpp
File metadata and controls
53 lines (48 loc) · 1.38 KB
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
48
49
50
51
52
53
#include <vector>
#include <string>
#include <iostream>
using namespace std;
class Solution {
vector<vector<int> > dp;
public:
vector<vector<string> > partition(string s) {
vector<vector<string> > result;
vector<string> vec;
dp = vector<vector<int> >(s.length(),vector<int>(s.length(),-1));
partition(s,0,s.length() - 1,result,vec);
return result;
}
void partition(string &s,int left,int right,vector<vector<string> > &result,vector<string> &vec){
if(left >= s.length()){
result.push_back(vec);
return;
}
for(int i = left;i <= right;i++){
if(dp[left][i] == 0) continue;
else if(dp[left][i] == 1 || (dp[left][i] == -1 && isPalindrome(s,left,i))){
dp[left][i] = 1;
vec.push_back(s.substr(left,i - left + 1));
partition(s,i + 1,right,result,vec);
vec.pop_back();
}
else{
dp[left][i] = 0;
continue;
}
}
}
bool isPalindrome(string &s,int left,int right){
while(left <= right){
if(s[left++] != s[right--]) return false;
}
return true;
}
};
int main(){
Solution s;
auto r = s.partition("aab");
for(auto v : r){
for(auto a : v) cout << a << " ";
cout << endl;
}
}