Distinct Subsequences LeetCode solution in JavaScript

1
var numDistinct = function (s, t) {
2
const n = s.length;
3

4
const memo = {};
5
const dfs = (index = 0, subsequence = "") => {
6
if (subsequence === t) return 1;
7
if (n - index + 1 < t.length - subsequence.length) return 0;
8
if (index === n) return 0;
9

10
const key = `${index}-${subsequence}`;
11
if (key in memo) return memo[key];
12

13
memo[key] =
14
t[subsequence.length] !== s[index]
15
? 0
16
: dfs(index + 1, subsequence + s[index]);
17
memo[key] += dfs(index + 1, subsequence);
18
return memo[key];
19
};
20

21
return dfs();
22
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0