Skip to content

Commit

Permalink
feat: 91. Decode Ways
Browse files Browse the repository at this point in the history
  • Loading branch information
HodaeSsi committed Dec 21, 2024
1 parent 9776d05 commit c9c25a2
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions decode-ways/HodaeSsi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution:
def numDecodings(self, s: str) -> int:
dp = []
if (s[0] == '0'):
return 0
dp.append(1)

for idx, _ in enumerate(s):
if idx == 0:
continue
if s[idx] == '0':
if s[idx-1] == '1' or s[idx-1] == '2':
if idx == 1:
dp.append(1)
else:
dp.append(dp[idx-2])
else:
return 0
elif s[idx-1] == '1' or (s[idx-1] == '2' and (s[idx] >= '1' and s[idx] <= '6')):
if idx == 1:
dp.append(2)
else:
dp.append(dp[idx-1] + dp[idx-2])
else:
dp.append(dp[idx-1])

return dp[-1]

0 comments on commit c9c25a2

Please sign in to comment.