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:
bool isPalindrome(string s, int begin, int end)
{
while (begin <= end)
{
if (s[begin] != s[end])
return false;
begin++;
end--;
}
return true;
}
vector<vector<string>> partition(string s) {
vector<vector<string> > res;
int pos = 0;
if (s.size() == 1) {
vector<string> tmp;
tmp.push_back(s);
res.push_back(tmp);
return res;
}
while (pos < s.size()) {
if (isPalindrome(s, 0, pos)) {
if (pos == s.size() - 1) {
vector<string> tmp;
tmp.push_back(s);
res.push_back(tmp);
}
else {
vector<vector<string> > subres = partition(s.substr(pos + 1));
for (int k = 0; k < subres.size(); ++k) {
subres[k].insert(subres[k].begin(), s.substr(0, pos + 1));
res.push_back(subres[k]);
}
}
}
pos++;
}
return res;
}
};