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<string>> partition(string s) {
vector<vector<string>> result;
vector<string> tmp;
part(s,0,tmp,result);
return result;
}
bool check(string s,int start,int end)
{
while(start<end)
{
if(s[start] != s[end])
return false;
start++;end--;
}
return true;
}
void part(string s,int start,vector<string>& tmp,vector<vector<string>>& result)
{
if(start == s.size())
{
result.push_back(tmp);
return;
}
for(int i=start;i<s.size();i++)
{
if(check(s,start,i))
{
tmp.push_back(s.substr(start,i-start+1));
part(s,i+1,tmp,result);
tmp.pop_back();
}
}
}
};