Longest palindrome¶

1) Increment on the letters of the string¶
2) For each letter, find the longest palindrome having this letter has center (and center left)¶
a) Do this bu increment the left and right side of the palindrome by 1 while the ends of the string are equal¶
3) Compare this palindrome to the global longest one¶
4) Return the longest palindrome¶
In [ ]:
def longestPalindrome(self, s: str) -> str:
    longest_palindrome, size = "", 0
    
    # i is the index of the current center of the palindrome tested
    for i in range(len(s)):
        
        # Palindrome with odd number of caracters (center is s[i])
        j = 1
        while i-j >= 0 and i+j < len(s) and s[i-j]==s[i+j]:
            j += 1
        j -= 1 # go back one step as the last checked element were different
        if 2 * j + 1 > size:
            size = 2 * j + 1
            longest_palindrome = s[i-j:i+j+1]

        # Palindrome with even number of caracters (center between s[i] and s[i])
        j = 1
        while i-j+1 >= 0 and i+j < len(s) and s[i-j+1]==s[i+j]:
            j += 1
        j -= 1
        if 2 * j > size:
            size = 2 * j
            longest_palindrome = s[i-j+1:i+j+1]

    return(longest_palindrome)