Created
July 3, 2018 00:35
-
-
Save changtimwu/166bc26a77ce223598d64875216c770b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def measure_palindrome(prefix,postfix): | |
i=0 | |
while i<len(prefix) and i<len(postfix): | |
if prefix[-i-1]!=postfix[i]: | |
break | |
i+=1 | |
return i | |
def longest_palindrome(s): | |
longest="" | |
for i in range(len(s)-1): | |
rlen = measure_palindrome( s[:i], s[i+1:]) | |
if rlen>0 and (rlen*2+1)>len(longest): | |
longest=s[i-rlen:i+rlen+1] | |
return longest | |
assert(longest_palindrome("aabcdcb")=="bcdcb") | |
assert(longest_palindrome("bananas")=="anana") | |
assert(longest_palindrome("aba")=="aba") | |
assert(longest_palindrome("ab")=="") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment