Simple solution with Python

  • 0

    Assume that string length is 5. So have to check
    s[0:5] - itself
    s[0:4] and s[1:5]
    s[0:3] , s[1:4] and s[2:5]
    s[0:2], s[1:3] , s[2:4] and s[3:5]
    s[0:1], s[1,2], s[2,3] , s[3,4] and s[4:5]
    if one of them is pali then return it. That it

        def longestPalindrome(self, s):
            l = len(s)
            if l == 1:
                return s
            for i in range(l):
                for j in range(i + 1):
                    es = s[j:(l - i + j)]
                    if es == es[::-1]:
                        return es
            return False

Log in to reply

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