Python code - TLE on the last testcase :(


  • 0
    B

    Could somebody help me identify why the following python code fails on the last test case? On looking through the discussion board, I feel like I'm more or less following the same algorithmic approach as AC solutions but missing some minor code optimizations.

    class Solution(object):
        def backtrack(self, output, result, s, start):
            if len(result) == 4 and start == len(s):
                output.append('.'.join(result))
                return
            
            if start >= len(s):
                return
            
            for i in xrange(start, start+3):
                segment = s[start:i+1]
                if int(segment) < 256 and (i+1-start == 1 or s[start] != "0"):
                    result.append(segment)
                    self.backtrack(output, result, s, i+1)
                    result.pop()
                else:
                    return
                
        def restoreIpAddresses(self, s):
            """
            :type s: str
            :rtype: List[str]
            """
            output, result = [], []
            self.backtrack(output, result, s, 0)
            return output
    

Log in to reply
 

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