描述
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
For example, given s = “aab”,
Return[
[“aa”,”b”],
[“a”,”a”,”b”]
]
分析
一个长度为n
的字符串有n - 1
个空隙,每个空隙可以选择切还是不切,所以共有2 ^ (n - 1)
种划分。使用回溯法,时间复杂度O(n*2^n)
,空间O(n)
。
代码
Python
1 | class Solution(object): |