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"] ]
// normal DFS.
#include <string>
#include <vector>
#include <iostream>
using namespace std;
bool isPalindrome(string tmp) {
int i = 0;
int j = tmp.size() - 1;
while(i <= j) {
if(tmp[i] == tmp[j]) {
i++;
j--;
} else {return false;}
}
return true;
}
void partition(string s, vector< vector<string> >& res, vector<string>& path) {
if(s.size() == 0) {
res.push_back(path);
return;
}
for(int i = 1; i <= s.size(); ++i) {
string tmp = s.substr(0, i);
if(isPalindrome(tmp)) {
path.push_back(tmp);
string remains = s.substr(i);
partition(remains, res, path);
path.pop_back();
}
}
}
vector< vector<string> > partition(string s) {
vector< vector<string> > res;
vector<string> path;
partition(s, res, path);
return res;
}
int main(void) {
vector< vector<string> > res = partition("aab");
for(int i = 0; i < res.size(); ++i) {
for(int j = 0; j < res[i].size(); ++j) {
cout << res[i][j] << endl;
}
}
}