Can anyone suggest how can I optimize this?


  • 0
    S
    class Solution(object):
        def longestPalindrome(self, s):
            """
            :type s: str
            :rtype: str
            """
            if s=="" or len(s)==1:
                return s
    
            l = list(s)
            m = 0
            i = 0
            while i<len(s):
                j = len(s)
                if len(l[i:len(s)])<m:
                    break
                while j:
                    s1 = "".join(l[i:j])
                    if len(l[i:j]) < m:
                        break
                    if str(s1)==str(s1)[::-1]:
                        if len(s1) > m:
                             pal=s1
                             m = len(pal)
                             break
                    j -=1
                i +=1
                
            return pal

Log in to reply
 

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