MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / numDistinct

Function numDistinct

javascript/0115-distinct-subsequences.js:10–30  ·  view source on GitHub ↗
(s, t, i = 0, j = 0, memo = initMemo(s, t))

Source from the content-addressed store, hash-verified

8 * @return {number}
9 */
10var numDistinct = (s, t, i = 0, j = 0, memo = initMemo(s, t)) => {
11 const isBaseCase1 = s.length < t.length;
12 if (isBaseCase1) return 0;
13
14 const isBaseCase2 = j === t.length;
15 if (isBaseCase2) return 1;
16
17 const isBaseCase3 = i === s.length;
18 if (isBaseCase3) return 0;
19
20 const hasSeen = memo[i][j] !== null;
21 if (hasSeen) return memo[i][j];
22
23 return dfs(
24 s,
25 t,
26 i,
27 j,
28 memo,
29 ); /* Time O(N * M) | Space O((N * M) + HEIGHT) */
30};
31
32var initMemo = (s, t) =>
33 new Array(s.length).fill().map(() => new Array(t.length).fill(null));

Callers 1

dfsFunction · 0.70

Calls 4

initMemoFunction · 0.70
dfsFunction · 0.70
initTabuFunction · 0.70
searchFunction · 0.70

Tested by

no test coverage detected