-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindrome_Partitioning.cpp
More file actions
47 lines (43 loc) · 1.16 KB
/
Palindrome_Partitioning.cpp
File metadata and controls
47 lines (43 loc) · 1.16 KB
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
/*
O(N!)
*/
class Solution {
public:
bool isPalindrome(string s)
{
int i=0,j=s.size()-1;
while(i<j)
{
if(s[i++]!=s[j--])
return false;
}
return true;
}
vector<vector<string>> partition(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<vector<string>> result;
for(int i=1;i<=s.size();i++)
{
string head=s.substr(0,i);
if(isPalindrome(head))
{
if(i==s.size())
{
vector<string> tmp;
tmp.push_back(head);
result.push_back(tmp);
return result;
}
string tail=s.substr(i,s.size()-i);
vector<vector<string>> sub=partition(tail);
for(int j=0;j<sub.size();j++)
{
sub[j].insert(sub[j].begin(),head);
result.push_back(sub[j]);
}
}
}
return result;
}
};