-
Notifications
You must be signed in to change notification settings - Fork 90
/
Palindrome Partitioning.py
44 lines (35 loc) · 1.17 KB
/
Palindrome Partitioning.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
"""
__author__ = 'Daniel'
class Solution:
def partition(self, s):
"""
:type s: str
:rtype: List[str]
"""
ret = []
self.backtrack(s, [], ret)
return ret
def backtrack(self, s, cur_lvl, ret):
"""
Expand the search tree horizontally.
i be the scanning index
If s[:i] is palindrome, then palindrome partition the s[i:]
:param s: the current temp string
:param cur_lvl: the current temp result at current level
:param ret: result
:return:
"""
if not s:
ret.append(list(cur_lvl))
for i in xrange(1, len(s)+1):
if self.predicate(s[:i]):
cur_lvl.append(s[:i])
self.backtrack(s[i:], cur_lvl, ret)
cur_lvl.pop()
def predicate(self, s):
return s == s[::-1]
if __name__ == "__main__":
assert Solution().partition("aabbc") == [['a', 'a', 'b', 'b', 'c'], ['a', 'a', 'bb', 'c'], ['aa', 'b', 'b', 'c'], ['aa', 'bb', 'c']]