Skip to content

Commit 3864ab5

Browse files
committed
decode ways solution
1 parent 0e7bd78 commit 3864ab5

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

decode-ways/krokerdile.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var numDecodings = function(s) {
6+
const n = s.length;
7+
const memo = {};
8+
9+
function dfs(index) {
10+
if (index === n) return 1;
11+
if (s[index] === '0') return 0;
12+
if (memo.hasOwnProperty(index)) return memo[index];
13+
14+
let count = dfs(index + 1);
15+
if (index + 1 < n) {
16+
const twoDigit = parseInt(s.slice(index, index + 2));
17+
if (twoDigit >= 10 && twoDigit <= 26) {
18+
count += dfs(index + 2);
19+
}
20+
}
21+
22+
memo[index] = count;
23+
return count;
24+
}
25+
26+
return dfs(0);
27+
};

0 commit comments

Comments
 (0)