-
Notifications
You must be signed in to change notification settings - Fork 889
/
DecodeWays.swift
35 lines (29 loc) · 955 Bytes
/
DecodeWays.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
/**
* Question Link: https://leetcode.com/problems/decode-ways/
* Primary idea: Dynamic Programming, dp[i] = dp[i - 1] + dp[i - 2],
* determine if current one or two characters are number at first
* Time Complexity: O(n), Space Complexity: O(n)
*
*/
class DecodeWays {
func numDecodings(_ s: String) -> Int {
let s = Array(s)
var dp = Array(repeating: 0, count: s.count + 1)
dp[0] = 1
for i in 1...s.count {
if s[i - 1] != "0" {
dp[i] += dp[i - 1]
}
if i > 1 && isValid(s, i - 2, i - 1) {
dp[i] += dp[i - 2]
}
}
return dp[s.count]
}
private func isValid(_ s: [Character], _ start: Int, _ end: Int) -> Bool {
guard let num = Int(String(s[start...end])) else {
fatalError()
}
return num >= 10 && num <= 26
}
}