forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwogha95.js
48 lines (43 loc) ยท 887 Bytes
/
wogha95.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
// TC: O(N)
// SC: O(N)
/**
* @param {string} s
* @return {number}
*/
var numDecodings = function (s) {
if (s[0] === "0") {
return 0;
}
if (s.length === 1) {
return 1;
}
const dpTable = new Array(s.length).fill(0);
if (s[0] !== "0") {
dpTable[0] = 1;
}
if (s[1] !== "0") {
dpTable[1] += 1;
}
if (isValid(`${s[0]}${s[1]}`)) {
dpTable[1] += 1;
}
for (let index = 2; index < s.length; index++) {
if (s[index] !== "0") {
dpTable[index] += dpTable[index - 1];
}
if (s[index - 1] !== "0" && isValid(`${s[index - 1]}${s[index]}`)) {
dpTable[index] += dpTable[index - 2];
}
}
return dpTable[dpTable.length - 1];
function isValid(stringNumber) {
const number = Number(stringNumber);
if (number <= 0) {
return false;
}
if (27 <= number) {
return false;
}
return true;
}
};