-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
decode-ways-ii.swift
74 lines (72 loc) · 2.64 KB
/
decode-ways-ii.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* https://leetcode.com/problems/decode-ways-ii/
*
*
*/
// Date: Sat Jul 10 12:05:38 PDT 2021
class Solution {
func numDecodings(_ s: String) -> Int {
let n = s.count
let mod = 1000000007
var dp = Array(repeating: 0, count: n + 1)
dp[0] = 1
let s = Array(s)
for index in 0 ..< n {
if s[index] == Character("*") {
dp[index + 1] = (dp[index] * 9) % mod
} else if s[index] != Character("0") {
dp[index + 1] = dp[index]
}
if index > 0 {
var flag = 0
switch (s[index - 1], s[index]) {
case (Character("1"), Character("*")):
flag = 9
case (Character("2"), Character("*")):
flag = 6
case (Character("2"), Character("1")):
flag = 1
case (Character("2"), Character("2")):
flag = 1
case (Character("2"), Character("3")):
flag = 1
case (Character("2"), Character("4")):
flag = 1
case (Character("2"), Character("5")):
flag = 1
case (Character("2"), Character("6")):
flag = 1
case (Character("2"), Character("0")):
flag = 1
case (Character("1"), Character("0")):
flag = 1
case (Character("1"), _):
flag = 1
case (Character("*"), Character("6")):
flag = 2
case (Character("*"), Character("5")):
flag = 2
case (Character("*"), Character("4")):
flag = 2
case (Character("*"), Character("3")):
flag = 2
case (Character("*"), Character("2")):
flag = 2
case (Character("*"), Character("1")):
flag = 2
case (Character("*"), Character("0")):
flag = 2
case (Character("*"), Character("*")):
flag = 9 + 6
case (Character("*"), _):
flag = 1
default:
flag = 0
}
dp[index + 1] += (dp[index - 1] * flag) % mod
dp[index + 1] %= mod
}
}
return dp[n]
}
}