class Solution {
public:
bool is_pal(string s, int start, int end)
{
while (start < end)
{
if (s.at(start) != s.at(end))
return false;
start++;
end--;
}
return true;
}
vector<vector<string>> part(string s, int start, int end)
{
vector<vector<string>> r;
for (int st = end; st >= 0; st--)
{
vector<vector<string>> ret;
if (is_pal(s, st, end))
{
string s_tmp = s.substr(st, end-st+1);
ret = part(s, start, st-1);
if (ret.size() != 0)
{
for (int i = 0; i < ret.size(); i++)
{
ret[i].push_back(s_tmp);
r.push_back(ret[i]);
}
}else{
vector<string> new_v;
new_v.push_back(s_tmp);
r.push_back(new_v);
}
}
}
return r;
}
vector<vector<string>> partition(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
return part(s, 0, s.size() -1);
}
};
![](https://img2024.cnblogs.com/blog/35695/202408/35695-20240826070647288-243310440.jpg)