-
Notifications
You must be signed in to change notification settings - Fork 0
/
131_partition_palidrome.py
68 lines (50 loc) · 1.17 KB
/
131_partition_palidrome.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
'''
131. Palindrome Partitioning
Medium
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
Example:
Input: "aab"
Output:
[
["aa","b"],
["a","a","b"]
]
definition::::
pal·in·drome
/ˈpalənˌdrōm/
a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run.
'''
def isPalindrome(s, start, end):
if start>end:
return False
# if start==end:
# return True
while start<end:
if s[start] is not s[end]:
return False
start=start+1
end = end-1
return True
import copy
def helper(s, start, out, res):
if start == len(s):
oo = copy.deepcopy(out)
res.append(oo)
return
for i in range(start, len(s)):
if isPalindrome(s, start, i) is False:
continue
out.append(s[start:i+1])
helper(s, i+1, out, res)
out.pop()
def partition(s):
if len(s) == 0:
return False
# print(isPalindrome(s, 0, len(s)-1))
res = []
out = []
helper(s, 0, out, res)
return res
s = 'a'
print(partition(s))