File tree 1 file changed +7
-6
lines changed
scripts/algorithms/P/Palindrome Partitioning
1 file changed +7
-6
lines changed Original file line number Diff line number Diff line change
1
+ # Runtime: 1302 ms (Top 13.95%) | Memory: 35.7 MB (Top 6.05%)
1
2
"""
2
3
we can approach this problem using manacher's algorithm with backtracking and recursion
3
4
"""
@@ -7,35 +8,35 @@ def partition(self, s: str) -> List[List[str]]:
7
8
def lps (s ):
8
9
if s in lookup :
9
10
return lookup [s ]
10
-
11
+
11
12
final_res = []
12
13
result_set = set ()
13
14
for k in range (len (s )):
14
15
i , j = k , k
15
-
16
+
16
17
# check for odd length palindromes
17
18
while i >= 0 and j < len (s ) and s [i ] == s [j ]:
18
19
# palindrome found
19
20
res = []
20
21
for partition in lps (s [:i ]):
21
22
res .append (partition + [s [i :j + 1 ]])
22
- for partition in res :
23
+ for partition in res :
23
24
for part in lps (s [j + 1 :]):
24
25
temp = partition + part
25
26
if tuple (temp ) not in result_set :
26
27
result_set .add (tuple (temp ))
27
28
final_res .append (temp )
28
29
i -= 1
29
30
j += 1
30
-
31
+
31
32
# check for even length palindromes
32
33
i , j = k , k + 1
33
34
while i >= 0 and j < len (s ) and s [i ] == s [j ]:
34
35
# palindrome found
35
36
res = []
36
37
for partition in lps (s [:i ]):
37
38
res .append (partition + [s [i :j + 1 ]])
38
- for partition in res :
39
+ for partition in res :
39
40
for part in lps (s [j + 1 :]):
40
41
temp = partition + part
41
42
if tuple (temp ) not in result_set :
@@ -45,4 +46,4 @@ def lps(s):
45
46
j += 1
46
47
lookup [s ] = final_res
47
48
return final_res
48
- return lps (s )
49
+ return lps (s )
You can’t perform that action at this time.
0 commit comments