-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathPalindrome Partitioning.py
49 lines (46 loc) · 1.85 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
45
46
47
48
49
# Runtime: 1302 ms (Top 13.95%) | Memory: 35.7 MB (Top 6.05%)
"""
we can approach this problem using manacher's algorithm with backtracking and recursion
"""
class Solution:
def partition(self, s: str) -> List[List[str]]:
lookup = {"": [[]]}
def lps(s):
if s in lookup:
return lookup[s]
final_res = []
result_set = set()
for k in range(len(s)):
i, j = k, k
# check for odd length palindromes
while i>= 0 and j < len(s) and s[i] == s[j]:
# palindrome found
res = []
for partition in lps(s[:i]):
res.append(partition + [s[i:j+1]])
for partition in res:
for part in lps(s[j+1:]):
temp = partition + part
if tuple(temp) not in result_set:
result_set.add(tuple(temp))
final_res.append(temp)
i-=1
j+=1
# check for even length palindromes
i, j = k, k+1
while i >= 0 and j < len(s) and s[i] == s[j]:
# palindrome found
res = []
for partition in lps(s[:i]):
res.append(partition + [s[i:j+1]])
for partition in res:
for part in lps(s[j+1:]):
temp = partition + part
if tuple(temp) not in result_set:
result_set.add(tuple(temp))
final_res.append(temp)
i-=1
j+=1
lookup[s] = final_res
return final_res
return lps(s)