Skip to content

Files

Latest commit

7856d6a · Sep 21, 2022

History

History

5-LongestPalindromicSubstring

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Sep 21, 2022
Sep 1, 2022

Longest Palindromic Substring

Problem can be found in here!

def longestPalindrome(s: str) -> str:
    longest_palindrome = s[0]
    memo = [[False] * len(s) for i in range(len(s))]

    for i in range(len(s)):
        memo[i][i] = True

    # P[i,j] = P(i+1,j-1) + S[i] == S[j]
    for i in range(len(s)):
        for j in range(i-1, -1, -1):
            if s[i] == s[j]:
                if i == j+1 or memo[i-1][j+1]:
                    memo[i][j] = True
                    if i-j+1 > len(longest_palindrome):
                        longest_palindrome = s[j:i+1]

    return longest_palindrome

Explanation: Consider the case "ababa". If we already knew that "bab" is a palindrome, it is obvious that "ababa" must be a palindrome since the two left and right end letters are the same.

The recursion function is P ( i , j ) = P ( i + i , j 1 ) + ( s [ i ] == s [ j ) ] , where P ( i , i ) = t r u e .

Time Complexity: O(n^2), Space Complexity: O(n^2)