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”]
]
class Solution {
public:
vector< vector<bool> > ok;
void dfs(vector< vector<string> > &ans, vector<string> tmp, int dep, int n, const string &s)
{
if(dep >= n)
{
ans.push_back(tmp);
}
for(int j=dep; j<n; j++)
{
if(ok[dep][j])
{
tmp.push_back(s.substr(dep, j-dep+1));
dfs(ans, tmp, j+1, n, s);
tmp.pop_back();
}
}
}
vector<vector<string>> partition(string s)
{
int n = s.size();
ok = vector< vector<bool> >(n, vector<bool>(n, false));
for(int i=n-1; i>=0; i--)
{
for(int j=i; j<n; j++)
{
if(s[i] == s[j] && (i+1 >= j-1 || ok[i+1][j-1]))
ok[i][j] = true;
}
}
vector< vector<string> > ans;
vector<string> tmp;
dfs(ans, tmp, 0, n, s);
return ans;
}
};