python
class Solution:
def restoreIpAddresses(self, s: str):
n = len(s)
track = []
res = []
index, count = 0, 0
def backtrack(s, index, track, count):
if count == 4 and index == n:
str = ''
for sr in track:
if (len(sr) > 1 and sr[0] == '0') or int(sr) > 255:
return
str += sr+'.'
res.append(str[:-1])
return
elif count == 4 or index >= n:
return
for length in range(1,4):
track.append(s[index:index+length])
index += length
count += 1
backtrack(s, index, track, count)
track.pop()
count -= 1
index -= length
backtrack(s, index, track, count)
return res