Question: Given a string s of length n , find the longest palindrome that is a substring of s in time ( | | ) O
Given a string s of length n find the longest palindrome that is a substring of s in time Os You may use copy over the implementation of Ukkonen's algorithm from the notes compute the suffix trie for s or the method from the previous problem even though it takes Os
def findlongestpalindromes:
transformed #join$formats # and $ are sentinels to avoid boundary issues
n lentransformed
P n # Pi will hold the radius of the palindrome centered at i
C R # C is the center of the rightmost palindrome, R is its right boundary
for i in range n : # We don't consider the first and last sentinels
mirror C i
if i R:
Pi minR i Pmirror
while transformedi Pi transformedi Pi:
Pi
if i Pi R:
C R i i Pi
maxlen, centerindex maxn i for i n in enumerateP
start centerindex maxlen # Compute the starting index in the original string
return sstart:start maxlen
t findlongestpalindromeABRACADABRA$
printt
assert t "ADA" or t "ACA"
IndexError Traceback most recent call last Cell In line t findlongestpalindromeABRACADABRA$ printt assert t "ADA" or t "ACA" Cell In line in findlongestpalindromes if i R: Pi minR i Pmirror while transformedi Pi transformedi Pi: Pi if i Pi R: IndexError: string index out of range
how do i fix this so that it takes into account the $ signs at the end of the abracadabra?
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
