-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path136.py
42 lines (39 loc) · 1.08 KB
/
136.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
class Solution:
"""
@param: s: A string
@return: A list of lists of string
"""
def partition(self, s):
# write your code here
if not s:
return
if len(s) == 1:
return [[s]]
res = []
substrMaps = self.substr(s)
for substrMap in substrMaps:
if not substrMap[1]:
res.append([substrMap[0]])
continue
nextsubstr = self.partition(substrMap[1])
for i in nextsubstr:
tmp = i[:]
tmp.insert(0, substrMap[0])
res.append(tmp)
return res
def substr(self, s):
if not s:
return
if len(s) == 1:
return [[s,""]]
res = []
for i in range(0, len(s)):
head, tail = 0, i
while head < tail:
if s[head] != s[tail]:
break
head += 1
tail -= 1
if head >= tail:
res.append([s[:i+1], s[i+1:]])
return res