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

Function minDistance

javascript/0072-edit-distance.js:9–25  ·  view source on GitHub ↗
(word1, word2, i = 0, j = 0)

Source from the content-addressed store, hash-verified

7 * @return {number}
8 */
9var minDistance = (word1, word2, i = 0, j = 0) => {
10 const isBaseCase1 = word1.length * word2.length === 0;
11 if (isBaseCase1) return word1.length + word2.length;
12
13 const isBaseCase2 = word1.length === i;
14 if (isBaseCase2) return word2.length - j;
15
16 const isBaseCase3 = word2.length === j;
17 if (isBaseCase3) return word1.length - i;
18
19 return dfs(
20 word1,
21 word2,
22 i,
23 j,
24 ); /* Time O(2^(N + M)) | Space O((N * M) + HEIGHT) */
25};
26
27var dfs = (word1, word2, i, j) => {
28 const isEqual = word1[i] === word2[j];

Callers 1

dfsFunction · 0.70

Calls 4

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

Tested by

no test coverage detected