class RestoreIPAddresses:
result = []
# @param s, a string
# @return a list of strings
def restoreIpAddresses(self, s):
if len(s) < 4 or len(s) > 12:
return self.result
current = ""
points = 3
times = 1
self.rec(s,len(s)1,points,current,times)
return self.result
def rec(self,s,index,points,current,times):
# check point number and currectness
if points == 0 and len(s[0:index]) > 3:
return
if times == 3 and int(s[index])*100 + int(s[index+1])*10 + int(s[index+2]) > 255:
return
# end
if index == 0:
if current.count('.') != 3:
return
current = s[index] + current
self.result.append(current[:])
return
if points > 0 and times <= 3:
# apply '.' at this position
self.rec(s,index1, points1,"." + s[index] + current, 1)
if times < 3:
# not apply '.' at this position
self.rec(s, index1, points, s[index] + current, times+1)
My Python solution, in my own environment it worked perfectly, why here I got :"output limit exceeded" ?


i think the recursion has caused the problem. leetcode usually tests for very large inputs and even though your code might eventually give the right answer it fails on leetcode since it is a sandboxed environment with security concerns. No one wants to keep servers busy to test your code