-
Notifications
You must be signed in to change notification settings - Fork 30
/
Palindrome Partitioning.cpp
50 lines (36 loc) · 1.23 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
44
45
46
47
48
49
50
/*
Solution by Rahul Surana
***********************************************************
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.
***********************************************************
*/
class Solution {
public:
vector<vector<string>> ans;
bool checkpalin(string s){
for(int i = 0; i < s.length()/2;i++){
if(s[i]!=s[s.length()-i-1]) return false;
}
return true;
}
void dff(int i, string& s, vector<string>& x){
if(i>=s.length()){
ans.push_back(x);
}
for(int k = i; k < s.length(); k++){
if(checkpalin(s.substr(i,k-i+1))){
// vector<string> z(x.begin(),x.end());
cout << s.substr(i,k-i+1) <<" ";
x.push_back(s.substr(i,k-i+1));
dff(k+1,s,x);
x.pop_back();
}
}
}
vector<vector<string>> partition(string s) {
vector<string> x;
dff(0,s,x);
return ans;
}
};