-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdecode-ways.py
More file actions
29 lines (29 loc) · 863 Bytes
/
decode-ways.py
File metadata and controls
29 lines (29 loc) · 863 Bytes
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
class Solution:
def numDecodings(self, s: str) -> int:
dp = [0] * len(s)
for i in range(0, len(s)):
if i == 0:
if s[i] == '0':
return 0
dp[i] = 1
continue
if s[i] == '0':
if s[i-1] != '1' and s[i-1] != '2':
return 0
if i >= 2:
dp[i] = dp[i-2]
else:
dp[i] = 1
continue
if s[i-1] == '0':
dp[i] = dp[i-1]
continue
if int(s[i-1:i+1]) <= 26:
if i >= 2:
dp[i] = dp[i-1] + dp[i-2]
else:
dp[i] = dp[i-1] + 1
continue
dp[i] = dp[i-1]
print(dp)
return dp[len(s)-1]