Python solution and have some questions over case of TLE.


  • 0
    M

    As I've come into the TLE situation when submitting to the server, I then started to make the code to be inside the TL.
    And some observations have been made.

    1. when the first two lines:
            if s == s[::-1]:
                return len(s)
    

    were deleted, it stuck at the "aaaa...aaaa" case. And I think the infamous case 'fff...ggg...ggg" case won't be affected with or without these two lines. Is that TRUE???

        def longestPalindromeSubseq(self, s):
            if s == s[::-1]:
                return len(s)
            n = len(s)
            r = [[0 for _ in xrange(n)] for _ in xrange(n)]
            for i in xrange(n-1, -1, -1):
                r[i][i] = 1
                for j in xrange(i+1, n):
                        r[i][j] = 2 + r[i+1][j-1] if s[i] == s[j] else max(r[i+1][j], r[i][j-1])
            return r[0][n-1]
    

Log in to reply
 

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