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 9279990 commit a3e09bdCopy full SHA for a3e09bd
decode-ways/doitduri.swift
@@ -0,0 +1,31 @@
1
+class Solution {
2
+ func numDecodings(_ s: String) -> Int {
3
+ if s.first == "0" {
4
+ return 0
5
+ }
6
+
7
+ if s.count == 1 {
8
+ return 1
9
10
11
+ var tables = Array(repeating: 0, count: s.count+1)
12
+ let chars = Array(s)
13
+ tables[0] = 1
14
+ tables[1] = 1
15
16
+ for i in 2...s.count {
17
+ if chars[i-1] != "0" {
18
+ tables[i] += tables[i-1]
19
20
21
+ if let twoDigit = Int(String(chars[i-2...i-1])) {
22
+ if 10 <= twoDigit && twoDigit <= 26 {
23
+ tables[i] += tables[i-2]
24
25
26
27
28
29
+ return tables[s.count]
30
31
+}
0 commit comments