【算法】分割回文串

难度:中等

题目:

给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是

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

示例 1:

输入:s = "aab"

输出:\["a","a","b","aa","b"]

示例 2:

输入:s = "a"

输出:\["a"]

提示:

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

解题思路:

使用回溯结合简单的回文检测来解决

  1. 定义辅助函数 isPalindrome:
  • 这个函数用于判断一个字符串是否为回文串。
  • 使用两个指针分别从字符串的头部和尾部向中心移动并比较字符是否相等。
  1. 定义递归函数 partition:
  • 参数包括:
    ■ s: 原始输入字符串。
    ■ start: 当前处理的子串起始位置。
    ■ path: 用于存储当前递归路径上的回文子串。
    ■ result: 最终结果数组,用于收集所有满足条件的分割方案。
  • 终止条件:当 start 等于字符串长度时,将 path 加入到结果数组 result 中。
  • 递归逻辑:
    • 遍历从 start 到字符串末尾的所有位置 i。
    • 如果从 start 到 i 的子串是回文串,则将其加入到 path 中。
    • 对剩余的字符串(从 i+1 开始)继续执行相同的操作。
    • 在递归结束后,从 path 中移除刚刚添加的子串,进行回溯。
  1. 调用 partition 函数:
  • 在主函数 partitionPalindromes 中,直接调用 partition 函数,无需额外参数,因为默认值已经设置好。

JavaScript 实现

javascript 复制代码
// 判断是不是回文串
function isPalindrome(str) {
    let left = 0, right = str.length - 1;
    while (left < right) {
        if (str[left] !== str[right]) {
            return false;
        }
        left++;
        right--;
    }
    return true;
}

// s是原始字符串   start: 当前处理的子串起始位置。path: 用于存储当前递归路径上的回文子串。result: 最终结果数组,用于收集所有满足条件的分割方案。
function partition(s, start = 0, path = [], result = null) {
    if (result === null) {
        result = [];
    }
    
    if (start === s.length) {
        result.push([...path]);
        return result;
    }
    
    for (let i = start; i < s.length; i++) {
        if (isPalindrome(s.slice(start, i + 1))) {
            path.push(s.slice(start, i + 1));
            partition(s, i + 1, path, result);
            path.pop(); // 回溯
        }
    }
    return result;
}

function partitionPalindromes(s) {
    return partition(s);
}

// 测试用例
// const testString = "aab";
// const result = partitionPalindromes(testString);
// console.log(result);
相关推荐
人间乄惊鸿客14 小时前
c++ 右值
c++·算法
fanstuck15 小时前
AI 客服答错了,三分钟分清是 Prompt 还是系统的锅
算法
田里的水稻15 小时前
FA_编程算法200个分类列数-SUM
算法·分类·数据挖掘
猿人谷18 小时前
从百万个 Pareto 解中选出 100 个:Riesz s-Energy 如何实现高效的多样性选择?
后端·算法·aigc
地平线开发者18 小时前
地平线 × 同济无人车队:PointPillars 从浮点模型到 BPU 部署
算法·自动驾驶
iiiiii1119 小时前
从最大熵到 SAC:Soft Q-Learning 与 Soft Actor-Critic 公式推导笔记
人工智能·笔记·算法·机器学习·强化学习
田里的水稻19 小时前
FA_排序算法-冒泡排序
java·算法·排序算法
AAA修煤气灶刘哥1 天前
AI 写的 SQL 跑通了,为什么和财务报表对不上?
人工智能·后端·算法