-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathleetcode-72-EditDistance.js
94 lines (87 loc) · 3.2 KB
/
leetcode-72-EditDistance.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* @param {string} word1
* @param {string} word2
* @return {number}
*/
// p: string , string;
// r: number;
// e:
//
//
// horse
// ros
// i = index: 01234
// i = length: 12345
// i
// horse
// j
// ros
// 125
// / | \
// i i i
// horse horse horse
// j j j
// ros ros ros
//
//
//
// (3) horse
// ros
// (remove) / |(insert) \(replace)
// (2) hors horse hors (3)
// ros ro ro
// (same) | / \ \ / / \
// (2) hor hors horse hors hor hors hor (2)
// ro ro r r ro r r
// (remove) / \ \ |
// (1) ho hor ho ho (2)
// ro r r x
// (same) |
// h (1)
// r
// / \ \ (replace)
// x h x (0)
// r x x
//
function minDistance(
word1,
word2,
len1 = word1.length,
len2 = word2.length,
memo = {}
) {
if (len1 === 0) return len2; // insert
if (len2 === 0) return len1; // remove
const len12 = len1 + "," + len2;
if (len12 in memo) return memo[len12];
let result;
// if characters at last index of both are the same
if (word1[len1 - 1] === word2[len2 - 1]) {
result = minDistance(word1, word2, len1 - 1, len2 - 1, memo);
} else {
const remove = minDistance(word1, word2, len1 - 1, len2, memo);
const insert = minDistance(word1, word2, len1, len2 - 1, memo);
const replace = minDistance(word1, word2, len1 - 1, len2 - 1, memo);
result = 1 + Math.min(remove, insert, replace);
}
memo[len12] = result;
return result;
}
// function minDistance(word1, word2, i = word1.length, j = word2.length, memo = {}) {
// if (i === 0) return j; // Operations to insert characters from word2 to an empty word1
// if (j === 0) return i; // Operations to remove characters from word1 to form an empty word2
// const ij = i + ',' + j;
// if (ij in memo) return memo[ij];
// let result;
// if (word1[i - 1] === word2[j - 1]) {
// result = minDistance(word1, word2, i - 1, j - 1, memo);
// } else {
// result = 1 + Math.min(
// minDistance(word1, word2, i - 1, j, memo), // Remove
// minDistance(word1, word2, i, j - 1, memo), // Insert
// minDistance(word1, word2, i - 1, j - 1, memo) // Replace
// );
// }
// memo[ij] = result;
// return result;
// }