Skip to content

Commit 212bab1

Browse files
Add files via upload
1 parent 395e844 commit 212bab1

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# 44ms 79.66%
2+
class Solution:
3+
def restoreIpAddresses(self, s):
4+
"""
5+
:type s: str
6+
:rtype: List[str]
7+
"""
8+
res_list = []
9+
def dfs(strs, temp_str):
10+
if not strs or temp_str.count('.') is 4:
11+
if not strs and temp_str.count('.') is 4:
12+
res_list.append(temp_str)
13+
return
14+
if strs[0] is '0':
15+
dfs(strs[1:], temp_str + '0.')
16+
else:
17+
dfs(strs[1:], temp_str + strs[:1] + '.')
18+
if len(strs) >= 2:
19+
dfs(strs[2:], temp_str + strs[:2] + '.')
20+
if len(strs) >= 3 and '100' <= strs[:3] <= '255':
21+
dfs(strs[3:], temp_str + strs[:3] + '.')
22+
dfs(s, '')
23+
return [strs[:-1] for strs in res_list]

0 commit comments

Comments
 (0)