Skip to content

Commit a3e09bd

Browse files
committed
Solution Decode ways
1 parent 9279990 commit a3e09bd

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

decode-ways/doitduri.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)