Skip to content

Commit e64125a

Browse files
Solve : Decode Ways
1 parent 791072c commit e64125a

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

decode-ways/printjin-gmailcom.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def numDecodings(self, s):
3+
if not s:
4+
return 0
5+
dp = [0] * (len(s) + 1)
6+
dp[0] = 1
7+
dp[1] = 1 if s[0] != '0' else 0
8+
for i in range(2, len(s) + 1):
9+
if '1' <= s[i - 1] <= '9':
10+
dp[i] += dp[i - 1]
11+
if '10' <= s[i - 2:i] <= '26':
12+
dp[i] += dp[i - 2]
13+
return dp[len(s)]

0 commit comments

Comments
 (0)