-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestoreIPAddress.py
39 lines (32 loc) · 1.01 KB
/
restoreIPAddress.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
"""
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(object):
def restoreIpAddresses(self, s):
"""
:type s: str
:rtype: List[str]
"""
def cutIP(s, n):
if n > len(s):
return []
if n == 1:
v = int(s)
if v > 255 or (v > 0 and s[0] == '0') or (v == 0 and len(s) > 1) :
return []
else:
return [s]
myIP = []
if s[0] == '0':
maxSegLen = 1
else:
maxSegLen = min(len(s) - n + 1, 3)
for i in xrange(1, maxSegLen + 1):
seg = int(s[:i])
if seg < 256:
myIP += [s[:i] + "." + p for p in cutIP(s[i:], n - 1)]
return myIP
return cutIP(s, 4)