forked from TonnyL/Windary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecodeWays.js
111 lines (94 loc) · 2.11 KB
/
DecodeWays.js
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
/**
* A message containing letters from A-Z is being encoded to numbers using the following mapping:
*
* 'A' -> 1
* 'B' -> 2
* ...
* 'Z' -> 26
* Given an encoded message containing digits, determine the total number of ways to decode it.
*
* For example,
* Given encoded message "12", it could be decoded as "AB" (1 2) or "L" (12).
*
* The number of ways decoding "12" is 2.
*
* Accepted.
*/
/**
* @param {string} s
* @return {number}
*/
let numDecodings = function (s) {
if (s == null || s.length === 0 || s.charAt(0) === '0') {
return 0;
}
let ints = new Int32Array(s.length + 1);
ints[0] = 1;
for (let i = 1; i < ints.length; i++) {
ints[i] = s.charAt(i - 1) === '0' ? 0 : ints[i - 1];
if (i > 1 && (s.charAt(i - 2) === '1' || (s.charAt(i - 2) === '2') && s.charAt(i - 1) <= '6')) {
ints[i] += ints[i - 2];
}
}
return ints[s.length];
};
// Expected: 1, ['A']
if (numDecodings("1") === 1) {
console.log("pass")
} else {
console.error("failed")
}
// Expected: 0
if (numDecodings("0") === 0) {
console.log("pass")
} else {
console.error("failed")
}
// Expected: 1, ['JA']
if (numDecodings("101") === 1) {
console.log("pass")
} else {
console.error("failed")
}
// Expected: 1, ['AK']
if (numDecodings("110") === 1) {
console.log("pass")
} else {
console.error("failed")
}
// Expected: 1, ['JJ']
if (numDecodings("1010") === 1) {
console.log("pass")
} else {
console.error("failed")
}
// Expected: 0
if (numDecodings("012") === 0) {
console.log("pass")
} else {
console.error("failed")
}
// Expected: 2, ['JAA', 'JK']
if (numDecodings("1011") === 2) {
console.log("pass")
} else {
console.error("failed")
}
// Expected: 2, ['AB', 'L']
if (numDecodings("12") === 2) {
console.log("pass")
} else {
console.error("failed")
}
// Expected: 3, ['ABC', 'LC', 'AW']
if (numDecodings("123") === 3) {
console.log("pass")
} else {
console.error("failed")
}
// Expected: 3, ['ABAT', 'LAT', 'AUT']
if (numDecodings("12120") === 3) {
console.log("pass")
} else {
console.error("failed")
}