Skip to content

Commit 3e6706a

Browse files
committed
decode ways
1 parent f0cf55f commit 3e6706a

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

decode-ways/samthekorean.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# TC : O(n)
2+
# SC : O(n)
3+
class Solution:
4+
def numDecodings(self, s: str) -> int:
5+
if not s or s[0] == "0":
6+
return 0
7+
8+
n = len(s)
9+
dp = [0] * (n + 1)
10+
dp[0] = 1
11+
dp[1] = 1
12+
13+
for i in range(2, n + 1):
14+
one_digit = int(s[i - 1])
15+
two_digits = int(s[i - 2 : i])
16+
17+
if one_digit != 0:
18+
dp[i] += dp[i - 1]
19+
20+
if 10 <= two_digits <= 26:
21+
dp[i] += dp[i - 2]
22+
23+
return dp[n]

0 commit comments

Comments
 (0)