We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f0cf55f commit 3e6706aCopy full SHA for 3e6706a
1 file changed
decode-ways/samthekorean.py
@@ -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