Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
For example, given s = "aab"
,
Return
[ ["aa","b"], ["a","a","b"] ]c++:
class Solution {
public:
vector<vector<string> > ret;
bool check(string s)
{
int size = s.size();
int i = 0;
int j = size - 1;
while(i <= j)
{
if(s[i] != s[j] )
{
return false;
}
i ++;
j --;
}
return true;
}
void parHelper(string s,int beg,int size,vector<string> &ans)
{
if(beg == size)
{
ret.push_back(ans);
return;
}
int i;
for(i = beg;i < size;i ++)
{
string tmp(s,beg,i-beg+1);
if(check(tmp))
{
ans.push_back(tmp);
parHelper(s,i+1,size,ans);
ans.pop_back();
}
}
}
vector<vector<string>> partition(string s) {
ret.clear();
int size = s.size();
if(size == 0)
{
return ret;
}
vector<string> ans;
parHelper(s,0,size,ans);
return ret;
}
};