131. 分割回文串https://leetcode.cn/problems/palindrome-partitioning/131. 分割回文串
难度中等1214
给你一个字符串 s
,请你将 s
分割成一些子串,使每个子串都是 回文串 。返回 s
所有可能的分割方案。
回文串 是正着读和反着读都一样的字符串。
示例 1:
输入:s = "aab" 输出:[["a","a","b"],["aa","b"]]
示例 2:
输入:s = "a" 输出:[["a"]]
提示:
1 <= s.length <= 16
s
仅由小写英文字母组成
通过次数216,656提交次数296,179
class Solution {
List<List<String>> ans = new ArrayList<List<String>>();
List<String> temp = new ArrayList<String>();
public List<List<String>> partition(String s) {
Combina(s,0);
return ans;
}
void Combina(String s,int index)
{
if(index==s.length())
{
ans.add(new ArrayList<>(temp));
return;
}
for(int i=index;i<s.length();i++)
{
if(huiwen(index,i,s)) temp.add(s.substring(index,i+1));
else continue;
Combina(s,i+1);
temp.remove(temp.size()-1);
}
}
boolean huiwen(int i,int j,String s)
{
for(;i<j;i++,j--)
{
if(s.charAt(i)!=s.charAt(j)) return false;
}
return true;
}
}