-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0091.Decode Ways.swift
40 lines (33 loc) · 953 Bytes
/
0091.Decode Ways.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Solution {
func numDecodings(_ s: String) -> Int {
let chars = Array(s)
let firstValid = isValid([chars[0]]) ? 1 : 0
if chars.count == 1 {
return firstValid
}
var dp = Array(repeating: 0, count: chars.count + 1)
dp[0] = 1
dp[1] = firstValid
for i in 2..<dp.count {
if isValid([chars[i-1]]) {
dp[i] += dp[i-1]
}
if isValid([chars[i-2],chars[i-1]]) {
dp[i] += dp[i-2]
}
}
return dp[dp.count-1]
}
private func isValid(_ num: [Character]) -> Bool {
if num.count < 0 || num.count > 2 {
return false
}
if num[0] == "0" {
return false
}
if let n = Int(String(num)), n >= 1, n <= 26 {
return true
}
return false
}
}