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;
if(s.size() == 0)
return result;
vector<string> path;
dfs(s,0,path,result);
return result;
}
void dfs(string s, int index, vector<string> path, vector<vector<string>> & result){
if(index >= s.length()){
result.push_back(path);
return;
}
for(int i = index; i <s.length(); i ++){
string temp = s.substr(index,i - index + 1);
if(isPal(temp)){
path.push_back(temp);
dfs(s,i + 1,path,result);
path.pop_back();
}
}
}
bool isPal(string temp){
if(temp.length() <= 1)
return true;
for(int i = 0; i < temp.length() / 2; i ++){
if(temp[i] != temp[temp.length() - i - 1])
return false;
}
return true;
}
};