-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0072-edit-distance.js
172 lines (135 loc) · 5.73 KB
/
0072-edit-distance.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
* Brute Force - DFS
* Time O(2^(N + M)) | Space O(N * M)
* https://leetcode.com/problems/edit-distance/
* @param {string} word1
* @param {string} word2
* @return {number}
*/
var minDistance = (word1, word2, i = 0, j = 0) => {
const isBaseCase1 = ((word1.length * word2.length) === 0);
if (isBaseCase1) return (word1.length + word2.length);
const isBaseCase2 = (word1.length === i);
if (isBaseCase2) return (word2.length - j);
const isBaseCase3 = (word2.length === j);
if (isBaseCase3) return (word1.length - i);
return dfs(word1, word2, i, j);/* Time O(2^(N + M)) | Space O((N * M) + HEIGHT) */
}
var dfs = (word1, word2, i, j) => {
const isEqual = (word1[i] === word2[j]);
if (isEqual) return minDistance(word1, word2, (i + 1), (j + 1));/* Time O(2^(N + M)) | Space O((N * M) + HEIGHT) */
const insert = minDistance(word1, word2, i, (j + 1)); /* Time O(2^(N + M)) | Space O((N * M) + HEIGHT) */
const _delete = minDistance(word1, word2, (i + 1), j); /* Time O(2^(N + M)) | Space O((N * M) + HEIGHT) */
const replace = minDistance(word1, word2, (i + 1), (j + 1)); /* Time O(2^(N + M)) | Space O((N * M) + HEIGHT) */
return (Math.min(insert, _delete, replace) + 1);
}
/**
* DP - Top Down
* Matrix - Memoization
* Time O(N * M) | Space O(N * M)
* https://leetcode.com/problems/edit-distance/
* @param {string} word1
* @param {string} word2
* @return {number}
*/
var minDistance = (word1, word2, i = 0, j = 0, memo = initMemo(word1, word2)) => {
const isBaseCase1 = ((word1.length * word2.length) === 0);
if (isBaseCase1) return (word1.length + word2.length);
const isBaseCase2 = (word1.length === i);
if (isBaseCase2) return (word2.length - j);
const isBaseCase3 = (word2.length === j);
if (isBaseCase3) return (word1.length - i);
const hasSeen = (memo[i][j] !== -1);
if (hasSeen) return memo[i][j];
return dfs(word1, word2, i, j, memo);/* Time O(N * M) | Space O((N * M) + HEIGHT) */
}
var initMemo = (word1, word2) => new Array(word1.length).fill()/* Time O(N) | Space O(N) */
.map(() => new Array(word2.length).fill(-1)); /* Time O(N) | Space O(N) */
var dfs = (word1, word2, i, j, memo) => {
const isEqual = (word1[i] === word2[j]);
if (isEqual) {
memo[i][j] = minDistance(word1, word2, (i + 1), (j + 1), memo);/* Time O(N * M) | Space O(HEIGHT) */
return memo[i][j];
}
const insert = minDistance(word1, word2, i, (j + 1), memo); /* Time O(N * M) | Space O(HEIGHT) */
const _delete = minDistance(word1, word2, (i + 1), j, memo); /* Time O(N * M) | Space O(HEIGHT) */
const replace = minDistance(word1, word2, (i + 1), (j + 1), memo); /* Time O(N * M) | Space O(HEIGHT) */
memo[i][j] = (Math.min(insert, _delete, replace) + 1); /* | Space O(N * M) */
return memo[i][j];
}
/**
* DP - Bottom Up
* Matrix - Tabulation
* Time O(N * M) | Space O(N * M)
* https://leetcode.com/problems/edit-distance/
* @param {string} word1
* @param {string} word2
* @return {number}
*/
var minDistance = (word1, word2) => {
const isEmpty = ((word1.length * word2.length) === 0);
if (isEmpty) return (word1.length + word2.length);
const tabu = initTabu(word1, word2);/* Time O(N * M) | Space O(N * M) */
search(word1, word2, tabu); /* Time O(N * M) | Space O(N * M) */
return tabu[word1.length][word2.length];
}
var initTabu = (word1, word2) => {
const tabu = new Array((word1.length + 1)).fill()/* Time O(N) | Space O(N) */
.map(() => new Array((word2.length + 1)).fill(0));/* Time O(M) | Space O(M) */
for (let i = 0; (i < (word1.length + 1)); i++) { /* Time O(N) */
tabu[i][0] = i; /* | Space O(N * M) */
}
for (let j = 0; (j < (word2.length + 1)); j++) { /* Time O(M) */
tabu[0][j] = j; /* | Space O(N * M) */
}
return tabu;
}
var search = (word1, word2, tabu) => {
for (let i = 1; (i < (word1.length + 1)); i++) {/* Time O(N) */
for (let j = 1; (j < (word2.length + 1)); j++) {/* Time O(M) */
const left = (tabu[(i - 1)][j] + 1);
const down = (tabu[i][(j - 1)] + 1);
const isEqual = (word1[(i - 1)] === word2[(j - 1)]);
const leftDown = (tabu[(i - 1)][(j - 1)] + Number(!isEqual));
tabu[i][j] = Math.min(left, down, leftDown); /* Space O(N * M) */
}
}
}
/**
* DP - Bottom Up
* Matrix - Tabulation
* Time O(N * M) | Space O(M)
* https://leetcode.com/problems/edit-distance/
* @param {string} word1
* @param {string} word2
* @return {number}
*/
var minDistance = (word1, word2) => {
const tabu = initTabu(word2);/* Time O(M) | Space O(M) */
search(word1, word2, tabu); /* Time O(N * M) | Space O(M) */
return tabu[word2.length];
}
var initTabu = (word2) => {
const tabu = new Array((word2.length + 1)).fill(0);/* Time O(M) | Space O(M) */
for (let j = 1; (j <= word2.length); j++) { /* Time O(M) */
tabu[j] = j; /* | Space O(M) */
}
return tabu;
}
var search = (word1, word2, tabu) => {
for (let i = 1; (i <= word1.length); i++) {/* Time O(N) */
tabu[word2.length] = update(word1, word2, i, tabu);/* Time O(M) | Space (M) */
}
}
const update = (word1, word2, i, tabu) => {
let temp = i;
for (let j = 1; (j <= word2.length); ++j) {/* Time O(M */
const isEqual = (word1[(i - 1)] === word2[(j - 1)])
const cur = isEqual
? tabu[(j - 1)]
: (Math.min(tabu[(j - 1)], tabu[j], temp) + 1);
tabu[(j - 1)] = temp; /* Space (M) */
temp = cur;
}
return temp;
}