-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0093M. Restore IP Addresses.py
More file actions
32 lines (31 loc) · 1.29 KB
/
0093M. Restore IP Addresses.py
File metadata and controls
32 lines (31 loc) · 1.29 KB
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
#Runtime: 28 ms, faster than 95.04% of Python3 online submissions for Restore IP Addresses.
#Memory Usage: 14.2 MB, less than 67.12% of Python3 online submissions for Restore IP Addresses.
class Solution:
def restoreIpAddresses(self, s: str) -> List[str]:
def legal_check(s):
if len(s) > 1 and s[0] == '0':
return False
if 0 <= int(s) <= 255:
return True
return False
if len(s) > 13 or len(s) < 4:
return []
if len(s) == 12:
cand = [s[:3], s[3:6], s[6:9], s[9:]]
for i in cand:
if not legal_check(i):
return []
return ['.'.join(cand)]
if len(s) == 4:
return ['.'.join(s)]
n = []
for i in range(1, 4):
for j in range(1, 4):
for k in range(1, 4):
if 0 < len(s)-i-j-k < 4:
n.append([i,j,k,len(s)-i-j-k])
cand = []
for i in n:
if legal_check(s[:i[0]]) and legal_check(s[i[0]:i[0]+i[1]]) and legal_check(s[i[0]+i[1]:i[0]+i[1]+i[2]]) and legal_check(s[i[0]+i[1]+i[2]:]):
cand += [s[:i[0]] + '.' + s[i[0]:i[0]+i[1]] + '.' + s[i[0]+i[1]:i[0]+i[1]+i[2]] + '.' + s[i[0]+i[1]+i[2]:]]
return cand