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>>res;
vector<string> tmp;
//判断是否是回文串
bool IsHuiWen(string s)
{
bool flag = true;
int left = 0;
int right = s.size() - 1;
while (left <= right)
{
if (s[left] != s[right])
{
flag = false;
break;
}
left++;
right--;
}
return flag;
}
void dfs(string s, int pos)
{
if (pos >= s.size())
{
res.push_back(tmp);
return;
}
for (int i = 1; i <= s.size() - pos; i++)
{
string sub = s.substr(pos, i);
if (IsHuiWen(sub))
{
tmp.push_back(sub);
dfs(s, pos + i);
tmp.pop_back();
}
}
}
vector<vector<string>> partition(string s)
{
dfs(s, 0);
return res;
}
};