131. Palindrome Partitioning
# Medium

Solution:
定义一个subs为string类型,相当于子集集合里的每个字符串,每次多加一个字母,判断是否是回文,不是的话继续加一个后面的字母,如果是回文,就加入子集集合中。若是碰到了return就回溯到是回文的状态,尝试别的可能的组合。
sub = '' 非常需要思考放在哪里?遗留问题。
helper函数的终止条件if index == len(s)
Input: "aabcd"
Stdout:
[["a","a","b","c","d"],["aa","b","c","d"]]Output:
level(before adding new element),subs(may be added to level)
[] a
['a'] a
['a', 'a'] b
['a', 'a', 'b'] c
['a', 'a', 'b', 'c'] d
['a', 'a', 'b'] cd
['a', 'a'] bc
['a', 'a'] bcd
['a'] ab
['a'] abc
['a'] abcd
[] aa
['aa'] b
['aa', 'b'] c
['aa', 'b', 'c'] d
['aa', 'b'] cd
['aa'] bc
['aa'] bcd
[] aab
[] aabc
[] aabcd
string = string[:-1] is to delete the last element of a string;
list.pop() is to delete the last element of a list
can sacrifice space to compute isParlindrome before backtracking, T = O(2^n)
Last updated
Was this helpful?