forked from kamyu104/LintCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
palindrome-partitioning.cpp
43 lines (39 loc) · 1.21 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
43
// Time: O(n * 2^n)
// Space: O(n)
class Solution {
public:
/**
* @param s: A string
* @return: A list of lists of string
*/
vector<vector<string>> partition(string s) {
vector<vector<string>> result;
vector<string> partition;
PalindromePartitioningHelper(s, 0, &partition, &result);
return result;
}
void PalindromePartitioningHelper(const string& s, size_t begin,
vector<string>* partition,
vector<vector<string>>* result) {
if (begin == s.size()) {
result->emplace_back(*partition);
return;
}
for (size_t i = begin + 1; i <= s.size(); ++i) {
string prefix = s.substr(begin, i - begin);
if (IsPalindrome(prefix)) {
partition->emplace_back(prefix);
PalindromePartitioningHelper(s, i, partition, result);
partition->pop_back();
}
}
}
bool IsPalindrome(const string& s) {
for (int i = 0, j = s.size() - 1; i < j; ++i, --j) {
if (s[i] != s[j]) {
return false;
}
}
return true;
}
};