11# reference :https://www.geeksforgeeks.org/dsa/longest-palindromic-substring/
22
3- def longest_palindromic_substring (s :str )-> str :
3+
4+ def longest_palindromic_substring (s : str ) -> str :
45 """
56 This function returns the longest palindromic substring in a string using dynamic programming
67 >>> longest_palindromic_substring("babad")
@@ -14,34 +15,35 @@ def longest_palindromic_substring(s:str)->str:
1415 >>> longest_palindromic_substring("")
1516 ''
1617 """
17- n = len (s )
18+ n = len (s )
1819
19- dp = [[False for i in range (n )] for j in range (n )]
20- start = 0
21- max_length = 1
20+ dp = [[False for i in range (n )] for j in range (n )]
21+ start = 0
22+ max_length = 1
2223
2324 for i in range (n ):
24- dp [i ][i ]= True
25-
25+ dp [i ][i ] = True
26+
2627 # for length 2 palindrome check
27- for i in range (n - 1 ):
28- if s [i ]== s [i + 1 ]:
29- dp [i ][i + 1 ] = True
30- start = i
31- max_length = 2
28+ for i in range (n - 1 ):
29+ if s [i ] == s [i + 1 ]:
30+ dp [i ][i + 1 ] = True
31+ start = i
32+ max_length = 2
3233
3334 # for length 3 and above
34- for length in range (3 ,n + 1 ):
35- for i in range (n - length + 1 ):
36- j = i + length - 1
37- if s [i ]== s [j ] and dp [i + 1 ][j - 1 ]:
38- dp [i ][j ]= True
39- start = i
40- max_length = length
35+ for length in range (3 , n + 1 ):
36+ for i in range (n - length + 1 ):
37+ j = i + length - 1
38+ if s [i ] == s [j ] and dp [i + 1 ][j - 1 ]:
39+ dp [i ][j ] = True
40+ start = i
41+ max_length = length
4142
42- return s [start : start + max_length ]
43+ return s [start : start + max_length ]
4344
44- def manacher_algorithm (s :str )-> str :
45+
46+ def manacher_algorithm (s : str ) -> str :
4547 """
4648 This function returns the longest palindromic substring in a string using Manacher's algorithm
4749 >>> longest_palindromic_substring("babad")
@@ -55,27 +57,28 @@ def manacher_algorithm(s:str)->str:
5557 >>> longest_palindromic_substring("")
5658 ''
5759 """
58- T = '^#' + '#' .join (s )+ '#$'
59- n = len (T )
60+ T = "^#" + "#" .join (s ) + "#$"
61+ n = len (T )
62+
63+ p = [0 ] * n
64+ c = 0
65+ r = 0
6066
61- p = [0 ]* n
62- c = 0
63- r = 0
67+ for i in range (1 , n - 1 ):
68+ mirror = 2 * c - i
69+ if i < r :
70+ p [i ] = min (r - i , p [mirror ])
71+ while T [i + (1 + p [i ])] == T [i - (1 + p [i ])]:
72+ p [i ] += 1
73+ if i + p [i ] > r :
74+ c = i
75+ r = i + p [i ]
6476
65- for i in range (1 ,n - 1 ):
66- mirror = 2 * c - i
67- if i < r :
68- p [i ]= min (r - i ,p [mirror ])
69- while T [i + (1 + p [i ])]== T [i - (1 + p [i ])]:
70- p [i ]+= 1
71- if i + p [i ]> r :
72- c = i
73- r = i + p [i ]
77+ max_length = max (p )
78+ max_center = p .index (max_length )
79+ start = (max_center - max_length ) // 2
80+ return s [start : start + max_length ]
7481
75- max_length = max (p )
76- max_center = p .index (max_length )
77- start = (max_center - max_length )// 2
78- return s [start :start + max_length ]
7982
8083if __name__ == "__main__" :
8184 import doctest
0 commit comments