Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def restoreIpAddresses(s: str):
- def backtrack(s, integers, path, result):
- if integers == 4:
- if not s:
- # we have 4 integers and
- # we've used all the digits in s
- result.append(path[:-1])
- return
- for i in range(1, 4):
- if i > len(s):
- continue
- # integer cannot have leading 0s
- if i > 1 and s[0] == '0':
- continue
- # integer must be less than 255
- if i > 2 and int(s[:3]) > 255:
- continue
- backtrack(s[i:], integers+1, path + s[:i] + '.', result)
- result = []
- backtrack(s, 0, '', result)
- return result
Advertisement
Advertisement
Advertisement
RAW Paste Data
Copied
Advertisement