-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path131_Palindrome_Partitioning.cpp
61 lines (53 loc) · 1.39 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
Example:
Input: "aab"
Output:
[
["aa","b"],
["a","a","b"]
]
*/
#include <vector>
#include <string>
using namespace std;
class Solution {
private:
void partition(string& s, int pos, vector<string>& path, \
vector<vector<bool>>& isPalindrome, \
vector<vector<string>>& res){
// check if end is reached
if(pos==(int)s.size()){
res.push_back(path);
return;
}
// add new palindrome
for(int i=pos; i<(int)s.size(); ++i){
if( isPalindrome[i][pos] ){// is substring of s in [pos, i] palindrome or not
path.push_back(s.substr(pos, i+1-pos)); // append substring to path
partition(s, i+1, path, isPalindrome, res);
path.pop_back(); // restore path after recursion
}
}
}
public:
vector<vector<string>> partition(string s) {
vector<vector<bool>> isPalindrome(s.size(), vector<bool>(s.size(), false));
vector<vector<string>> res;
vector<string> path;
// fill in isPalindrome(O(n^2))
for(int i=0; i<(int)s.size(); ++i){
for(int j=0; j<=i; ++j){
if( (s[i]==s[j])&& \
((i-j<2)||(isPalindrome[i-1][j+1])) ){
isPalindrome[i][j] = true;// substring of s in [j, i] is palindrome
}
}
}
// partition recursively(O(n*2^n))
partition(s, 0, path, isPalindrome, res);
// return
return res;
}
};