Skip to content

Commit

Permalink
decode ways
Browse files Browse the repository at this point in the history
  • Loading branch information
SamTheKorean committed Jul 12, 2024
1 parent f0cf55f commit 3e6706a
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions decode-ways/samthekorean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# TC : O(n)
# SC : O(n)
class Solution:
def numDecodings(self, s: str) -> int:
if not s or s[0] == "0":
return 0

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

for i in range(2, n + 1):
one_digit = int(s[i - 1])
two_digits = int(s[i - 2 : i])

if one_digit != 0:
dp[i] += dp[i - 1]

if 10 <= two_digits <= 26:
dp[i] += dp[i - 2]

return dp[n]

0 comments on commit 3e6706a

Please sign in to comment.