【算法】分割回文串

难度:中等

题目:

给你一个字符串 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);
相关推荐
forest_0925 分钟前
《AI 编程 Harness 实战手册》04|审批这道门:点了一百次“允许”,还剩多少作用?
人工智能·算法·prompt·agi
H.莓飛43 分钟前
【C++】【STL】stack_OJ题
服务器·c语言·开发语言·数据结构·c++·算法·visual studio
不会武功不懂江湖3 小时前
RISC-V链接器与链接脚本
汇编·算法·risc-v
Zwawa4 小时前
卡尔曼到底在算什么:从一维到它的变式以及工程版卡尔曼滤波算法C语言实现
c语言·算法·嵌入式
电科_银尘4 小时前
【大模型算法与工程 14】Tokenizer与Embedding:文本如何变成模型输入
算法·embedding
饮水桶_T5 小时前
Python 面向对象(十七)|继承、封装、多态 笔记
数据结构·笔记·算法
地平线开发者8 小时前
让 AI Agent 和 征程6 台架高效、稳定、安全地协作
算法·安全·自动驾驶
kyle~9 小时前
SHA-256 哈希去重
算法·哈希算法
风落青芜12 小时前
链式二叉树的实现与基本操作
c语言·开发语言·数据结构·算法