-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathPalindrome Partitioning
More file actions
53 lines (42 loc) · 992 Bytes
/
Palindrome Partitioning
File metadata and controls
53 lines (42 loc) · 992 Bytes
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
// Shreyansh Shukla
// 21
// Input : s = "aabcb"
// Output: [["a","a","b","c","b"],["a","a","bcb"],["aa","b","c","b"],["aa","bcb"]]
class Solution {
public:
vector<vector<string>>ans;
bool isPalindrom(int i,int j,string str)
{
while(i<=j)
{
if(str[i] != str[j])
return false;
i++;
j--;
}
return true;
}
void fun(string &s,vector<string> &v,int index=0)
{
if(index == s.size())
{
ans.push_back(v);
return;
}
for(int i=index;i<s.size();i++)
{
if(isPalindrom(index,i,s))
{
v.push_back(s.substr(index,i-index+1));
fun(s,v,i+1);
v.pop_back();
}
}
}
vector<vector<string>> partition(string s)
{
vector<string>v;
fun(s,v);
return ans;
}
};