Skip to content

Commit

Permalink
feat: add decode ways solution
Browse files Browse the repository at this point in the history
  • Loading branch information
mangodm-web committed Aug 24, 2024
1 parent 8771f03 commit cf50f90
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions decode-ways/mangodm-web.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution:
def numDecodings(self, s: str) -> int:
if s[0] == "0":
return 0

n = len(s)
dp = [0] * (n + 1)
dp[0], dp[1] = 1, 1

for i in range(2, n + 1):
if s[i - 1] != "0":
dp[i] += dp[i - 1]
if "10" <= s[i - 2 : i] <= "26":
dp[i] += dp[i - 2]

return dp[n]

0 comments on commit cf50f90

Please sign in to comment.