forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
restore-ip-addresses.py
41 lines (36 loc) · 1.32 KB
/
restore-ip-addresses.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from __future__ import print_function
# Time: O(n^m) = O(3^4)
# Space: O(n * m) = O(3 * 4)
#
# Given a string containing only digits, restore it by returning all possible valid IP address combinations.
#
# For example:
# Given "25525511135",
#
# return ["255.255.11.135", "255.255.111.35"]. (Order does not matter)
#
class Solution:
# @param s, a string
# @return a list of strings
def restoreIpAddresses(self, s):
result = []
self.restoreIpAddressesRecur(result, s, 0, "", 0)
return result
def restoreIpAddressesRecur(self, result, s, start, current, dots):
# pruning to improve performance
if (4 - dots) * 3 < len(s) - start or (4 - dots) > len(s) - start:
return
if start == len(s) and dots == 4:
result.append(current[:-1])
else:
for i in xrange(start, start + 3):
if len(s) > i and self.isValid(s[start:i + 1]):
current += s[start:i + 1] + '.'
self.restoreIpAddressesRecur(result, s, i + 1, current, dots + 1)
current = current[:-(i - start + 2)]
def isValid(self, s):
if len(s) == 0 or (s[0] == '0' and s != "0"):
return False
return int(s) < 256
if __name__ == "__main__":
print(Solution().restoreIpAddresses("0000"))