Leetcode练习(Python):回溯算法类:第131题:分割回文串:给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串。 返回 s 所有可能的分割方案。

题目:
分割回文串:给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串。  返回 s 所有可能的分割方案。
思路:
使用回溯算法的模板。
程序:
class Solution:
    def partition(self, s: str) -> List[List[str]]:
        if not s:
            return []
        auxiliary = []
        result = []
        def backtrack(s, auxiliary, result):
            if not s:
                result.append(auxiliary[:])
                return
            for index in range(1, len(s) + 1):
                if s[: index] == s[index - 1 : : -1]:
                    auxiliary.append(s[: index])
                    backtrack(s[index :], auxiliary, result)
                    auxiliary.pop()
        backtrack(s, auxiliary, result)
        return result
原文地址:https://www.cnblogs.com/zhuozige/p/12871095.html