LeetCode--131.分割回文串(回溯算法)

131.分割回文串

题目描述

给你一个字符串 s,请你将 s 分割成一些 子串,使每个子串都是 回文串 。返回 s 所有可能的分割方案。

示例 1:

输入:s = "aab"
输出:[["a","a","b"],["aa","b"]]

示例 2:

输入:s = "a"
输出:[["a"]]

提示:

  • 1 <= s.length <= 16
  • s 仅由小写英文字母组成

代码

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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值