- Notifications
You must be signed in to change notification settings - Fork 625
/
Copy path93.py
34 lines (28 loc) · 1017 Bytes
/
93.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
'''
Given a string containing only digits, restore it by returning all possible valid IP address combinations.
Example:
Input: "25525511135"
Output: ["255.255.11.135", "255.255.111.35"]
'''
classSolution(object):
defrestoreIpAddresses(self, s):
"""
:type s: str
:rtype: List[str]
"""
result= []
defdfs(s, temp, count):
ifcount==4:
ifnots:
result.append(temp[:-1])
return
forindexinrange(1, 4):
ifindex<=len(s):
ifindex==1:
dfs(s[index:], temp+s[:index] +".", count+1)
elifindex==2ands[0] !='0':
dfs(s[index:], temp+s[:index] +".", count+1)
elifindex==3ands[0] !='0'andint(s[:3]) <=255:
dfs(s[index:], temp+s[:index] +".", count+1)
dfs(s, "", 0)
returnresult