Given A string s, find the longest palindromic substring in S. Assume that the maximum length of S is 1000.
Example:
Input: "Babad" Output: "Bab" note: "ABA" is also a valid answer.
Example:
Input: "CBBD" Output: "BB"
1 classSolution:2 defLongestpalindrome (self, s):3 """4 : Type S:str5 : Rtype:str6 """7 #preprocess8Slist =list (s)9 forIinchRange ((len (s) + 1) * 2) [:: 2]:TenSlist.insert (I,"#") OneSlist.insert (0,'$') A -p =Self.manacher (slist) - thei =P.index (Max (p)) -Ans ="'. Join (slist[i-p[i]+1:i+P[i]]) - returnAns.replace ('#',"'). Replace ('$',"') - defManacher (Self, slist): + - + #Calculate P Ap = [0] *Len (slist) atP[0] = 1 -ID =0 -MX = 1 - Print(slist) - forIinchRange (1, Len (slist)): - ifMX >I: inP[i] = min (P[id * 2-i], MX-i) - Else: toP[i] = 1 + - #violence the whileI +p[i]<len (slist) andSlist[i-p[i]]==slist[i +P[i]]: *P[i] = p[i]+1 $ #Update maximum ternary groupPanax Notoginseng if(MX < i +P[i]): -MX = i +P[i] theID =I + A returnP
5. Longest palindromic Substring (longest palindrome substring manacher algorithm)