Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Using difflib solution in Uncategorized category for The Longest Palindromic by macfreek
import difflib
def find_longest_match(a, b):
sm = difflib.SequenceMatcher(None, a, b)
i, j, k = sm.find_longest_match(0, len(a), 0, len(b))
return a[i:i+k] # equal to b[j:j+k]
def longest_palindromic(text):
inverse_text = text[::-1]
return find_longest_match(text, inverse_text)
if __name__ == '__main__':
assert longest_palindromic("artrartrt") == "rtrartr", "The Longest"
assert longest_palindromic("abacada") == "aba", "The First"
assert longest_palindromic("aaaa") == "aaaa", "The A"
Feb. 21, 2018
Comments: