-
Notifications
You must be signed in to change notification settings - Fork 20
/
RestoreIPAddresses.py
36 lines (32 loc) · 1.21 KB
/
RestoreIPAddresses.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
# 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)
#
# Python, Python3 all accepted.
class RestoreIPAddresses:
def restoreIpAddresses(self, s):
"""
:type s: str
:rtype: List[str]
"""
length = len(s)
results = []
if length < 4 or length > 12:
return results
for i in range(1, 4):
for j in range(1, 4):
for k in range(1, 4):
for m in range(1, 4):
if i + j + k + m == length:
a = int(s[0:i])
b = int(s[i:j + i])
c = int(s[i + j:k + i + j])
d = int(s[i + j + k:m + i + j + k])
if a <= 255 and b <= 255 and c <= 255 and d <= 255:
string = "" + str(a) + "." + str(b) + "." + str(c) + "." + str(d)
if len(string) == length + 3:
results.append(string)
return results