-
Notifications
You must be signed in to change notification settings - Fork 12
/
DecodeWaysII.cpp
74 lines (69 loc) · 2.13 KB
/
DecodeWaysII.cpp
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
74
class Solution {
public:
int numDecodings(string s) {
int num = pow(10, 9) + 7, n = s.size();
if(n == 0){
return 0;
}
if(s[0] == '0'){
return 0;
}
vector<long int> temp(n+1, 1);
if(s[0] == '*'){
temp[1] = 9;
}
for(int i = 2; i <= n; i++){
if(s[i-1] == '*'){
temp[i] = temp[i-1]*9;
temp[i] = temp[i]%num;
if(s[i-2] == '*'){
temp[i] += temp[i-2]*15;
}
else if(s[i-2] == '1'){
temp[i] += temp[i-2]*9;
}
else if(s[i-2] == '2'){
temp[i] += temp[i-2]*6;
}
temp[i] = temp[i]%num;
}
else{
if(s[i-1] == '0'){
if(s[i-2] == '*'){
temp[i] = temp[i-2]*2;
}
else if(s[i-2] == '1' || s[i-2] == '2'){
temp[i] = temp[i-2];
}
else{
return 0;
}
}
else{
temp[i] = temp[i-1];
temp[i] = temp[i]%num;
if(s[i-2] == '1'){
temp[i] += temp[i-2];
}
else if(s[i-2] == '2'){
int curr = s[i-1] - '0';
if(curr >= 0 && curr <= 6){
temp[i] += temp[i-2];
}
}
else if(s[i-2] == '*'){
int curr = s[i-1] - '0';
if(curr >= 0 && curr <= 6){
temp[i] += temp[i-2]*2;
}
else{
temp[i] += temp[i-2];
}
}
}
temp[i] = temp[i]%num;
}
}
return (int)temp[n];
}
};