-
Notifications
You must be signed in to change notification settings - Fork 614
/
93.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"]
'''
class Solution(object):
def restoreIpAddresses(self, s):
"""
:type s: str
:rtype: List[str]
"""
result = []
def dfs(s, temp, count):
if count == 4:
if not s:
result.append(temp[:-1])
return
for index in range(1, 4):
if index <= len(s):
if index == 1:
dfs(s[index:], temp + s[:index] + ".", count+1)
elif index ==2 and s[0] != '0':
dfs(s[index:], temp + s[:index] + ".", count+1)
elif index == 3 and s[0] != '0' and int(s[:3]) <= 255:
dfs(s[index:], temp + s[:index] + ".", count+1)
dfs(s, "", 0)
return result