20 lines python code using DP but TLE


  • 0
    C
    class Solution(object):
    def longestPalindrome(self, s):
        """
        :type s: str
        :rtype: str
        """
        s_rev = s[::-1]
        max, loc = (0, 0)
        length = len(s)
        D0 = []
        for row in xrange(length+1): D0 += [[0]*(length+1)]
        for i in range(length):
            for j in range(length):
                if s[i] is s_rev[j]:
                    Tmp = D0[i+1][j+1] = D0[i][j] + 1
                    if max <= Tmp:
                        max, loc = Tmp, i
        return s[loc-max+1:loc+1]
    

    Any suggestion?


Log in to reply
 

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