Skip to content

Commit c9c25a2

Browse files
committed
feat: 91. Decode Ways
1 parent 9776d05 commit c9c25a2

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

decode-ways/HodaeSsi.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution:
2+
def numDecodings(self, s: str) -> int:
3+
dp = []
4+
if (s[0] == '0'):
5+
return 0
6+
dp.append(1)
7+
8+
for idx, _ in enumerate(s):
9+
if idx == 0:
10+
continue
11+
if s[idx] == '0':
12+
if s[idx-1] == '1' or s[idx-1] == '2':
13+
if idx == 1:
14+
dp.append(1)
15+
else:
16+
dp.append(dp[idx-2])
17+
else:
18+
return 0
19+
elif s[idx-1] == '1' or (s[idx-1] == '2' and (s[idx] >= '1' and s[idx] <= '6')):
20+
if idx == 1:
21+
dp.append(2)
22+
else:
23+
dp.append(dp[idx-1] + dp[idx-2])
24+
else:
25+
dp.append(dp[idx-1])
26+
27+
return dp[-1]
28+

0 commit comments

Comments
 (0)