-
Notifications
You must be signed in to change notification settings - Fork 522
/
Copy path14. Palindrome Partitioning.cpp
67 lines (56 loc) · 1.19 KB
/
14. 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
62
63
64
65
66
67
/*
Palindrome Partitioning
=======================
Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s.
A palindrome string is a string that reads the same backward as forward.
Example 1:
Input: s = "aab"
Output: [["a","a","b"],["aa","b"]]
Example 2:
Input: s = "a"
Output: [["a"]]
Constraints:
1 <= s.length <= 16
s contains only lowercase English letters.
*/
class Solution
{
bool palendrome(string s)
{
int i = 0, j = s.size() - 1;
while (i < j)
{
if (s[i] != s[j])
return false;
i++;
j--;
}
return true;
}
void solution(string s, vector<string> asf, vector<vector<string>> &ans)
{
if (s.size() == 0)
{
ans.push_back(asf);
return;
}
for (int i = 0; i < s.size(); ++i)
{
auto prefix = s.substr(0, i + 1);
auto rest = s.substr(i + 1);
if (palendrome(prefix))
{
asf.push_back(prefix);
solution(rest, asf, ans);
asf.pop_back();
}
}
}
public:
vector<vector<string>> partition(string s)
{
vector<vector<string>> ans;
solution(s, {}, ans);
return ans;
}
};