【回溯】131. 分割回文串

题目:

给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串。

返回 s 所有可能的分割方案。

示例: 输入: "aab" 输出: [ ["aa","b"], ["a","a","b"] ]

题解:

切割字符串类似于组合,可以画成二叉树的形式。

步骤:

1.递归函数参数

定义全局变量path存放切割后回文的子串,res存放结果集。 (这两个参数可以放到函数参数里)

本题递归函数参数还需要startIndex,因为切割过的地方,不能重复切割,和组合问题也是保持一致的。

2.递归函数终止条件

从树形结构的图中可以看出:切割线切到了字符串最后面,说明找到了一种切割方法,此时就是本层递归的终止终止条件。

3.单层搜索的逻辑

首先判断这个子串是不是回文,如果是回文,就加入path中,path用来记录切割过的回文子串。

判断回文: 

    public boolean isPalindrome(String s, int start, int end) {
        for (int i = start, j = end; i < j; i++, j--) {
            if (s.charAt(i) != s.charAt(j)) {
                return false;
            }
        }
        return true;
    }

完整代码: 

class Solution {
    LinkedList<String> path = new LinkedList<>();
    List<List<String>> res = new LinkedList<>();

    public List<List<String>> partition(String s) {
        backtracking(s, 0);
        return res;
    }

    public void backtracking(String s, int startIndex) {
        if (startIndex == s.length()) {
            res.add(new LinkedList<>(path));
            return;
        }
        for (int i = startIndex; i < s.length(); i++) {
            if (isPalindrome(s, startIndex, i)) { //注意isPalindrome函数后两个参数是什么,检查的是同一个树层的回文情况
                String str = s.substring(startIndex, i + 1); //substring左闭右开
                path.add(str);
            } else {
                continue;
            }
            backtracking(s, i + 1);
            path.remove(path.size() - 1);
        }
    }

    public boolean isPalindrome(String s, int start, int end) {
        for (int i = start, j = end; i < j; i++, j--) {
            if (s.charAt(i) != s.charAt(j)) {
                return false;
            }
        }
        return true;
    }
}

参考:代码随想录 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个有趣的问题,使用 Kotlin 可以很方便地解决。 我的思路是使用回溯法,遍历所有可能的分割方式,并判断每个子串是否为文串。 具体实现细节如下: 首先定义一个函数 `isPalindrome` 用于判断一个字符串是否为文串,这个函数的实现非常简单,只需要判断字符串正着读和反着读是否相等即可。 ```kotlin fun isPalindrome(str: String): Boolean { return str == str.reversed() } ``` 接下来定义一个函数 `partition` 用于遍历所有可能的分割方式。这个函数的参数包括原始字符串 `s`,当前处理到的位置 `start`,当前已经分割出来的子串列表 `cur`,以及最终的结果列表 `res`。 在这个函数中,我们首先判断当前位置是否已经到达字符串末尾。如果是,就把当前的分割方案添加到结果列表中。否则,我们从当前位置开始往后遍历,尝试分割出一个文子串。如果找到了一个文子串,我们就把它添加到子串列表中,然后递归处理后面的子串。最后,记得要回溯到上一个状态,把添加的子串从列表中移除。 ```kotlin fun partition(s: String, start: Int, cur: MutableList<String>, res: MutableList<List<String>>) { if (start == s.length) { res.add(cur.toList()) return } for (i in start until s.length) { val str = s.substring(start, i + 1) if (isPalindrome(str)) { cur.add(str) partition(s, i + 1, cur, res) cur.removeAt(cur.lastIndex) } } } ``` 最后,我们可以在主函数中调用 `partition` 函数,并返结果列表。完整代码如下: ```kotlin fun isPalindrome(str: String): Boolean { return str == str.reversed() } fun partition(s: String, start: Int, cur: MutableList<String>, res: MutableList<List<String>>) { if (start == s.length) { res.add(cur.toList()) return } for (i in start until s.length) { val str = s.substring(start, i + 1) if (isPalindrome(str)) { cur.add(str) partition(s, i + 1, cur, res) cur.removeAt(cur.lastIndex) } } } fun partition(s: String): List<List<String>> { val res = mutableListOf<List<String>>() partition(s, 0, mutableListOf(), res) return res } ``` 你可以使用以下代码测试这个函数: ```kotlin fun main() { val s = "aab" val res = partition(s) println(res) } ``` 输出结果为: ``` [[a, a, b], [aa, b]] ``` 这说明我们的代码已经正确地找到了字符串所有可能的文子串分割方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值