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 f46f265 commit dfa0f77Copy full SHA for dfa0f77
decode-ways/delight010.swift
@@ -0,0 +1,19 @@
1
+class Solution {
2
+ func numDecodings(_ s: String) -> Int {
3
+ var array = Array(s)
4
+ var dp: [Int: Int] = [array.count: 1]
5
+ for i in stride(from: array.count - 1, to: -1, by: -1) {
6
+ if array[i] == "0" {
7
+ dp[i] = 0
8
+ } else {
9
+ dp[i] = dp[i + 1]
10
+ }
11
+
12
+ if i + 1 < array.count && (array[i] == "1" || array[i] == "2" && "0123456".contains(array[i + 1])) {
13
+ dp[i, default: 0] += dp[i + 2] ?? 0
14
15
16
+ return dp[0]!
17
18
+}
19
0 commit comments