Why TLE??? My solution is fast enough in my computer and the time complexity is O(n^2)


  • 0
    T
    class Solution:
        # @param {string} s
        # @return {string}
        def longestPalindrome(self, s):
            l = len(s)
            v = [[] for i in range(l)]
            for j in range(l):
                v[j]=[0 for i in range(l)] 
            for i in range(l):
                for j in range(l-i):
                    if i==0:
                        v[j][i+j] = 1
                        continue
                    if i==1:
                        flag=1
                        if s[j]==s[i+j]:
                            v[j][i+j] = 1
                        continue
                    if v[j+1][j+i-1] ==1 and s[j] == s[j+i]:
                        v[j][i+j]=1
            maxNumber = 0
            start = 0
            i=l-1
            while i>=0:
                for j in range(l-i):
                    if v[j][i+j]==1 and maxNumber<i+1:
                        maxNumber = i+1
                        start = j
                        break
                i-=1
            return s[start:start+maxNumber]

Log in to reply
 

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