Simple python solution


  • 0
    D

    you do not need to verify the s is odd length or even length

    class Solution(object):
        def longestPalindrome(self, s):
            """
            :type s: str
            :rtype: str
            """
            mx, n, res = 0,len(s), []
            for i in range(n*2 - 1):
                left = i/2
                right = left + i%2
                while left >= 0 and right < n and s[left] == s[right]:
                    left -= 1
                    right += 1
                if mx < right - left - 1:
                    res = s[left + 1:right]
                    mx = right - left - 1
            return res
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.