diff --git a/decode-ways/HC-kang.ts b/decode-ways/HC-kang.ts new file mode 100644 index 000000000..7e835a321 --- /dev/null +++ b/decode-ways/HC-kang.ts @@ -0,0 +1,31 @@ +// T.C: O(n) +// S.C: O(n) +function numDecodings(s: string): number { + const NUM_OF_ALPHA = 26; + const memo = new Map(); + + function dfs(idx: number): number { + if (idx === s.length) { + return 1; + } + if (s[idx] === '0') { + return 0; + } + if (memo.has(idx)) { + return memo.get(idx)!; + } + + let count = dfs(idx + 1); + if ( + idx + 2 <= s.length && // check if idx + 2 is in the range + parseInt(s.slice(idx, idx + 2), 10) <= NUM_OF_ALPHA + ) { + count += dfs(idx + 2); + } + + memo.set(idx, count); + return count; + } + + return dfs(0); +} \ No newline at end of file