-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0639-decode-ways-ii.js
More file actions
61 lines (50 loc) · 1.6 KB
/
0639-decode-ways-ii.js
File metadata and controls
61 lines (50 loc) · 1.6 KB
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
/**
* Decode Ways II
* Time Complexity: O(N)
* Space Complexity: O(1)
*/
var numDecodings = function (s) {
const moduloValue = 1e9 + 7;
let waysEndingCurrent = 1;
let waysEndingWithOne = 0;
let waysEndingWithTwo = 0;
let calculatedTotalWays = 0;
let nextWaysEndingWithOne = 0;
let nextWaysEndingWithTwo = 0;
for (const stringElement of s) {
calculatedTotalWays = 0;
nextWaysEndingWithOne = 0;
nextWaysEndingWithTwo = 0;
if (stringElement === "*") {
calculatedTotalWays =
(calculatedTotalWays + 9 * waysEndingCurrent) % moduloValue;
calculatedTotalWays =
(calculatedTotalWays + 9 * waysEndingWithOne) % moduloValue;
calculatedTotalWays =
(calculatedTotalWays + 6 * waysEndingWithTwo) % moduloValue;
nextWaysEndingWithOne = waysEndingCurrent;
nextWaysEndingWithTwo = waysEndingCurrent;
} else {
if (stringElement !== "0") {
calculatedTotalWays =
(calculatedTotalWays + waysEndingCurrent) % moduloValue;
}
calculatedTotalWays =
(calculatedTotalWays + waysEndingWithOne) % moduloValue;
if (stringElement <= "6") {
calculatedTotalWays =
(calculatedTotalWays + waysEndingWithTwo) % moduloValue;
}
if (stringElement === "1") {
nextWaysEndingWithOne = waysEndingCurrent;
}
if (stringElement === "2") {
nextWaysEndingWithTwo = waysEndingCurrent;
}
}
waysEndingCurrent = calculatedTotalWays;
waysEndingWithOne = nextWaysEndingWithOne;
waysEndingWithTwo = nextWaysEndingWithTwo;
}
return waysEndingCurrent;
};