Python solution by Dynamic Programming but it is time excceed. Please tell me how to modify and improve this code. Thank you.


  • 0
    def longestPalindrome(self,s):
        savelist = list()
        flag = 0
        def maxPal(flag, savelist):
            relist = list()
            charlist = list()
            for char in range(flag, len(s)):
                charlist.append(s[char])
                relist = [] + charlist
                relist.reverse()
                if relist == charlist and len(charlist) > len(savelist):
                    savelist = [] + charlist       
            if flag < len(s) - 1:
                return maxPal(flag + 1, savelist)
            if flag >= len(s) - 1:
                return "".join(savelist)
        return maxPal(flag, savelist)
    

Log in to reply
 

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