-
Notifications
You must be signed in to change notification settings - Fork 8
/
palindrome-partitioning.cpp
43 lines (40 loc) · 1.06 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
// 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"]
// ]
//
//
class Solution {
public:
bool isPalindrome(string s) {
for (int i = 0, len = s.size(); i < len/2; ++i) {
if (s[i] != s[len-1-i]) return false;
}
return true;
}
vector<vector<string>> partition(string s) {
vector<vector<string>> ans;
for (int i = 1;i <= s.size();++i) {
string substring = s.substr(0, i);
if (!isPalindrome(substring)) continue;
vector<vector<string>> subAns = partition(s.substr(i));
for (int j = 0; j < subAns.size(); ++j) {
subAns[j].insert(subAns[j].begin(), substring);
ans.push_back(subAns[j]);
}
if (subAns.size() == 0) {
ans.push_back(vector<string>(1,substring));
}
}
return ans;
}
};