-
Notifications
You must be signed in to change notification settings - Fork 0
/
Palindrome Partitioning
31 lines (31 loc) · 983 Bytes
/
Palindrome Partitioning
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
class Solution {
public:
bool checkPalindrome(string str, int startIndex, int lastIndex){
while(startIndex <= lastIndex){
if(str[startIndex] != str[lastIndex])
return false;
startIndex++;
lastIndex--;
}
return true;
}
void palindromePartition(int index, vector<string>& ds, vector<vector<string>>& output, string str){
if(index == str.length()){
output.push_back(ds);
return;
}
for(int i = index; i < str.length(); i++){
if(checkPalindrome(str, index, i)){
ds.push_back(str.substr(index, i - index + 1));
palindromePartition(i+1, ds, output, str);
ds.pop_back();
}
}
}
vector<vector<string>> partition(string s) {
vector<vector<string>> output;
vector<string> ds;
palindromePartition(0, ds, output, s);
return output;
}
};