131.分割回文串
题目描述
给你一个字符串 s,请你将 s 分割成一些 子串,使每个子串都是 回文串 。返回 s 所有可能的分割方案。
示例 1:
输入:s = "aab"
输出:[["a","a","b"],["aa","b"]]
示例 2:
输入:s = "a"
输出:[["a"]]
提示:
1 <= s.length <= 16s仅由小写英文字母组成
代码
java
class Solution {
// 存放最终结果
// result 中的每一个 List<String> 都是一种分割方案
List<List<String>> result = new ArrayList<>();
// 用来记录当前路径(当前已经切割出的回文子串)
List<String> path = new LinkedList<>();
/**
* 回溯函数
*
* @param s 原字符串
* @param startIndex 当前切割开始的位置
*/
void backtracking(String s, int startIndex){
// 终止条件:
// 如果 startIndex 来到了字符串末尾
// 说明已经完成了一种切割方案
if(startIndex == s.length()){
result.add(new ArrayList<>(path));
return;
}
// 从 startIndex 开始尝试切割
for(int i = startIndex; i < s.length(); i++){
// 判断 s[startIndex ~ i] 是否为回文串
if(isPalindrome(s, startIndex, i)){
// 如果是回文串,则加入当前路径
path.add(s.substring(startIndex, i + 1));
// 继续递归,下一次从 i+1 开始切割
backtracking(s, i + 1);
// 回溯:撤销本次选择
path.removeLast();
}
}
}
/**
* 判断一个字符串区间是否为回文串
*
* @param s 原字符串
* @param start 左指针
* @param end 右指针
* @return 是否为回文串
*/
boolean isPalindrome(String s, int start, int end){
// 双指针向中间靠拢
while(start <= end){
// 只要有一个字符不同,就不是回文串
if(s.charAt(start++) != s.charAt(end--)){
return false;
}
}
// 全部字符都相同,说明是回文串
return true;
}
/**
* 主函数
* 返回字符串所有可能的回文分割方案
*/
public List<List<String>> partition(String s) {
// 从下标 0 开始回溯
backtracking(s, 0);
return result;
}
}