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”]
]
用递归的思想求解,DP表示不会。。。。。。
相关问题:
void dfs(string s, vector<string> &path, vector<vector<string>> &result)
{
if(s.size() < 1)
{
result.push_back(path);
return ;
}
for(int i = 0; i < s.size(); ++i)
{
int start = 0;
int end = i;
while(start < end)
{
if(s[start] == s[end])
{
++start;
--end;
}
else break;
}
if(start >= end)
{
path.push_back(s.substr(0, i+1));
dfs(s.substr(i+1), path, result);
path.pop_back();
}
}
}
vector<vector<string>> partition(string s)
{
vector<vector<string>> result;
vector<string> path;
dfs(s, path, result);
return result;
}