-
Notifications
You must be signed in to change notification settings - Fork 1
/
decode-ways.py
41 lines (40 loc) · 1.06 KB
/
decode-ways.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
37
38
39
40
41
# DFS, Time Limit Exceed
class Solution:
def numDecodings(self, s):
"""
:type s: str
:rtype: int
"""
res = []
def dfs(s, path):
if not s and path:
res.append(path[:])
return
for i in range(1, len(s) + 1):
n = int(s[:i])
if 1 <= n <= 26:
dfs(s[i:], path + [n])
else:
break
dfs(s, [])
return len(res)
# DP
class Solution:
def numDecodings(self, s):
"""
:type s: str
:rtype: int
"""
if not s or s.startswith("0"):
return 0
dp = [1,1]
for i in range(2, len(s) + 1):
if 10 <= int(str(s[i-2:i])) <= 26 and s[i-1] != '0':
dp.append(dp[i-1] + dp[i-2])
elif int(s[i-2:i]) == 10 or int(s[i-2:i]) == 20:
dp.append(dp[i-2])
elif s[i-1] != "0":
dp.append(dp[i-1])
else:
return 0
return dp[-1]