Skip to content

Commit de5164c

Browse files
committed
decode ways solutions
1 parent 9c60339 commit de5164c

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

decode-ways/devyejin..py

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

0 commit comments

Comments
 (0)