【LeetCode热题100(59/100)】分割回文串

题目地址:链接

思路: 依然是简单的递归回溯(枚举所有组合,逐个检查组合中的每个字符串是否为回文串)

/**
 * @param {string} s
 * @return {string[][]}
 */

let isP = (arr) => {
    for(let str of arr) {
        let n = str.length;
        let m = Math.floor(str.length / 2);
        for(let i = 0; i < m; i ++) {
            if(str[i] !== str[n - 1 - i]) {
                return false;
            }
        }
    }
    return true;
}
var partition = function(s) {
    let ans = [];
    let n = s.length;
    const dfs = (idx, arr) => {
        if(idx == n) {
            ans = [...arr];
            return;
        }
        
        let arrLen = arr.length;
        for(let i = 0; i < arrLen; i ++) {
            let subarr = arr[i];
            arr.push([...subarr, s[idx]]);
            let m = subarr.length;
            subarr[m - 1] += s[idx];
        }
        if(arr.length == 0) arr.push([s[idx]]);
        dfs(idx + 1, arr);
    }

    dfs(0, [])

    let res = [];
    for(const subAns of ans) {
        if(isP(subAns)) res.push(subAns);
    }

    return res;
};
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值