给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串。
返回 s 所有可能的分割方案。
示例:
输入: "aab"
输出:
[
["aa","b"],
["a","a","b"]
]
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/palindrome-partitioning
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
class Solution {
public:
vector<vector<string>> partition(string s) {
vector<vector<string>> res;
vector<string> out;
DFS(s,0,out,res);
return res;
}
void DFS(string s,int start,vector<string>& out,vector<vector<string>>& res){
if(start==s.size()){
res.push_back(out);
return;
}
for(int i=start;i<s.size();i++){
if(!isPalindrom(s,start,i)) continue;
out.push_back(s.substr(start,i-start+1));
DFS(s,i+1,out,res);
out.pop_back();
}
}
bool isPalindrom(string s,int start,int end){
while(start<end){
if(s[start]!=s[end]) return false;
start++;
end--;
}
return true;
}
};