Why is this python solution giving TLE?


  • 2
    C
    def shortestPalindrome(self, s):
        start = 0
        end = len(s)-1
        lastValidEnd = end
        while start<end:
            if s[start]==s[end]:
                start += 1
                end -= 1
            else:
                start = 0
                lastValidEnd -= 1
                end = lastValidEnd
        return s[len(s)-1:lastValidEnd:-1]+s

  • 0
    K

    Because it's O(n^2) solution.


Log in to reply
 

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