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 0f731bc commit 40601beCopy full SHA for 40601be
decode-ways/hyunjung-choi.kt
@@ -0,0 +1,27 @@
1
+class Solution {
2
+ fun numDecodings(s: String): Int {
3
+ if (s.isEmpty() || s[0] == '0') return 0
4
+
5
+ val n = s.length
6
+ val dp = IntArray(n + 1)
7
8
+ dp[0] = 1
9
+ dp[1] = 1
10
11
+ for (i in 2..n) {
12
+ val currentChar = s[i - 1]
13
+ val prevChar = s[i - 2]
14
15
+ if (currentChar != '0') {
16
+ dp[i] += dp[i - 1]
17
+ }
18
19
+ val twoDigit = (prevChar - '0') * 10 + (currentChar - '0')
20
+ if (twoDigit in 10..26) {
21
+ dp[i] += dp[i - 2]
22
23
24
25
+ return dp[n]
26
27
+}
0 commit comments