Skip to content

Commit

Permalink
Kotlin: 91. Decode Ways
Browse files Browse the repository at this point in the history
  • Loading branch information
MaratKhakim committed Oct 1, 2022
1 parent 18ee23d commit cb8e1b1
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions kotlin/91-Decode-Ways.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
fun numDecodings(s: String): Int {
val n = s.length
val dp = IntArray(n+1)
dp[0] = 1
dp[1] = if (s[0] == '0') 0 else 1

for (i in 2..n) {
val curr = s[i-1] - '0'
val prev = s[i-2] - '0'
val num = 10*prev + curr

if (curr > 0)
dp[i] += dp[i-1]

if (num >= 10 && num <= 26)
dp[i] += dp[i-2]
}

return dp[n]
}
}

0 comments on commit cb8e1b1

Please sign in to comment.