Skip to content

Commit 865dc65

Browse files
committed
Runtime: 1302 ms (Top 13.95%) | Memory: 35.7 MB (Top 6.05%)
1 parent 7f47573 commit 865dc65

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# Runtime: 1302 ms (Top 13.95%) | Memory: 35.7 MB (Top 6.05%)
12
"""
23
we can approach this problem using manacher's algorithm with backtracking and recursion
34
"""
@@ -7,35 +8,35 @@ def partition(self, s: str) -> List[List[str]]:
78
def lps(s):
89
if s in lookup:
910
return lookup[s]
10-
11+
1112
final_res = []
1213
result_set = set()
1314
for k in range(len(s)):
1415
i, j = k, k
15-
16+
1617
# check for odd length palindromes
1718
while i>= 0 and j < len(s) and s[i] == s[j]:
1819
# palindrome found
1920
res = []
2021
for partition in lps(s[:i]):
2122
res.append(partition + [s[i:j+1]])
22-
for partition in res:
23+
for partition in res:
2324
for part in lps(s[j+1:]):
2425
temp = partition + part
2526
if tuple(temp) not in result_set:
2627
result_set.add(tuple(temp))
2728
final_res.append(temp)
2829
i-=1
2930
j+=1
30-
31+
3132
# check for even length palindromes
3233
i, j = k, k+1
3334
while i >= 0 and j < len(s) and s[i] == s[j]:
3435
# palindrome found
3536
res = []
3637
for partition in lps(s[:i]):
3738
res.append(partition + [s[i:j+1]])
38-
for partition in res:
39+
for partition in res:
3940
for part in lps(s[j+1:]):
4041
temp = partition + part
4142
if tuple(temp) not in result_set:
@@ -45,4 +46,4 @@ def lps(s):
4546
j+=1
4647
lookup[s] = final_res
4748
return final_res
48-
return lps(s)
49+
return lps(s)

0 commit comments

Comments
 (0)