Simple Python Solution


  • 0
    S
    class Solution(object):
        def longestPalindrome(self, s):
            """
            :type s: str
            :rtype: str
            """
            n = len(s)
            max_len = 0
            max_p = ""
            for i in range(n):
                for j in range(i, i + 2):
                    b = i
                    e = j
                    while b >= 0 and e < n and s[b] == s[e]:
                        b -= 1
                        e += 1
                    l = e - b - 1
                    if l > max_len:
                        max_len = l
                        max_p = s[b + 1 : e]
            return max_p
    

Log in to reply
 

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