5. Longest Palindromic Substring

Medium

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.

😇 Solution

class Solution:
    
    def isPalindrome(s):
        return s == s[::-1]
     
    def longestPalindrome(self, s: str) -> str:
        
        max_length = 0
        ans = ''
        
        for i in range(len(s)):
            for j in range(i+1, len(s)+1):
                temp = s[i:j]
                if Solution.isPalindrome(temp) and len(temp) > max_length:
                    max_length = len(temp)
                    ans = temp
        return ans
        

Last updated

Was this helpful?