-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Decode Ways II.cpp
205 lines (174 loc) · 5.79 KB
/
Decode Ways II.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
Company Tags : Facebook
Leetcode Link : https://leetcode.com/problems/decode-ways-ii/
*/
//Approach-1 (Recur + Memo) (TLE)
class Solution {
public:
int n;
int M= 1e9+7;
int t[1000001];
unordered_map<string, int> mp;
long long solve(string s, int idx) {
if(idx > n)
return 0;
if(idx == n)
return 1;
if(s[idx] == '0')
return 0;
if(t[idx] != -1)
return t[idx];
long long result = ((s[idx] == '*' ? 9 : 1 ) * solve(s, idx+1))%M;
if(idx < n-1) {
string last2 = s.substr(idx, 2); //get two digit number
if((last2[0] == '*' || last2[1] == '*')) {
result = (result+mp[last2]*solve(s, idx+2))%M;
} else {
if(s[idx] == '1' || (s[idx] == '2' && s[idx+1] < '7'))
result = (result + solve(s, idx+2))%M;
}
}
return t[idx] = result;
}
int numDecodings(string s) {
mp["**"] = 15; //{11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 24, 25, 26}
mp["1*"] = 9; //{11, 12, 13, 14, 15, 16, 17, 18, 19}
mp["2*"] = 6; //{21, 22, 23, 24, 25, 26}
mp["*0"] = 2; //{10, 20}
mp["*1"] = 2; //{11, 21}
mp["*2"] = 2; //{12, 22}
mp["*3"] = 2; //{13, 23}
mp["*4"] = 2; //{14, 24}
mp["*5"] = 2; //{15, 25}
mp["*6"] = 2; //{16, 26}
mp["*7"] = 1; //{17}
mp["*8"] = 1; //{18}
mp["*9"] = 1; //{19}
memset(t, -1, sizeof(t));
n = s.length();
return solve(s, 0);
}
};
//Approach-2 (Recur + Memo without map : Accepted)
class Solution {
public:
long mod = 1e9+7;
int n;
long solve(vector<long> &t, string &s, int idx) {
if (idx == n)
return 1;
if (idx > s.length() || s[idx] == '0')
return 0;
if (t[idx] != -1)
return t[idx];
long last1 = solve(t, s, idx + 1) * (s[idx] == '*' ? 9 : 1);
long last2 = 0;
if (idx < n-1 && s[idx] < '3') {
if (s[idx] == '*' && s[idx + 1] == '*')
last2 = 15;
else if (s[idx] == '*')
last2 = s[idx + 1] < '7' ? 2 : 1;
else if (s[idx + 1] == '*')
last2 = s[idx] == '1' ? 9 : 6;
else {
int num = stoi(s.substr(idx, 2));
last2 = num <= 26 ? 1 : 0;
}
last2 *= last2 ? solve(t, s, idx + 2) : 0;
}
return t[idx] = (last1 + last2) % mod;
}
int numDecodings(string s) {
n = s.length();
vector<long> t(s.size(), -1);
return solve(t, s, 0);
}
};
//Approach-3 (Bottom Up DP)
class Solution {
public:
int M= 1e9+7;
int numDecodings(string s) {
static unordered_map<string, int> mp;
mp["**"] = 15; //{11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 24, 25, 26}
mp["1*"] = 9; //{11, 12, 13, 14, 15, 16, 17, 18, 19}
mp["2*"] = 6; //{21, 22, 23, 24, 25, 26}
mp["*0"] = 2; //{10, 20}
mp["*1"] = 2; //{11, 21}
mp["*2"] = 2; //{12, 22}
mp["*3"] = 2; //{13, 23}
mp["*4"] = 2; //{14, 24}
mp["*5"] = 2; //{15, 25}
mp["*6"] = 2; //{16, 26}
mp["*7"] = 1; //{17}
mp["*8"] = 1; //{18}
mp["*9"] = 1; //{19}
int n = s.length();
vector<long long> t(n+1, 0);
t[n] = 1;
for(int i = n-1; i>=0; i--) {
if(s[i] == '0')
t[i] = 0;
else {
t[i] = (s[i] == '*' ? 9 : 1) * t[i+1];
if(i < n-1) {
string last2 = s.substr(i, 2); //get two digit number
if((last2[0] == '*' || last2[1] == '*')) {
t[i] = (t[i] + mp[last2]*t[i+2]) % M;
} else {
if(s[i] == '1' || (s[i] == '2' && s[i+1] < '7'))
t[i] = (t[i] + t[i+2]) % M;
}
}
}
}
return (int)t[0];
}
};
//Approach-4 (Bottom Up O(1) space)
class Solution {
public:
int M = 1e9+7;
int numDecodings(string s) {
static unordered_map<string, int> mp;
mp["**"] = 15; //{11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 24, 25, 26}
mp["1*"] = 9; //{11, 12, 13, 14, 15, 16, 17, 18, 19}
mp["2*"] = 6; //{21, 22, 23, 24, 25, 26}
mp["*0"] = 2; //{10, 20}
mp["*1"] = 2; //{11, 21}
mp["*2"] = 2; //{12, 22}
mp["*3"] = 2; //{13, 23}
mp["*4"] = 2; //{14, 24}
mp["*5"] = 2; //{15, 25}
mp["*6"] = 2; //{16, 26}
mp["*7"] = 1; //{17}
mp["*8"] = 1; //{18}
mp["*9"] = 1; //{19}
int n = s.length();
if(s[0] == '0')
return 0;
if(n == 1)
return s[0] == '*' ? 9 : 1;
long last1 = 1, last2 = 1;
for(int i = n-1; i>=0; i--) {
int count = 0;
if(s[i] == '0')
count = 0;
else {
count = (count + last1*(s[i] == '*' ? 9 : 1)) % M;
if(i < n-1) {
string temp = s.substr(i, 2);
if(temp[0] == '*' || temp[1] == '*')
count = (count + last2*mp[temp]) % M;
else {
if(s[i] == '1' || (s[i] == '2' && s[i+1] < '7'))
count = (count + last2) % M;
}
}
}
last2 = last1;
last1 = count;
}
return (int)last1;
}
};