-
Notifications
You must be signed in to change notification settings - Fork 130
/
Edit Distance.js
43 lines (38 loc) · 1.1 KB
/
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
/**
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)
You have the following 3 operations permitted on a word:
a) Insert a character
b) Delete a character
c) Replace a character
* @param {string} word1
* @param {string} word2
* @return {number}
*/
var minDistance = function(word1, word2) {
var len1 = word1.length,
len2 = word2.length,
matrix = [],
i,
j;
if(len1 === 0 || len2 === 0) {
return Math.max(len1, len2);
}
//initialization
for(i = 0; i <= len1; i++) {
matrix[i] = [];
matrix[i][0] = i;
}
for (j = 0; j <= len2; j++) {
matrix[0][j] = j;
}
for (i = 1; i <= len1; i++) {
for (j = 1; j <= len2; j++) {
if (word1.charAt(i - 1) === word2.charAt(j - 1)) {
matrix[i][j] = matrix[i - 1][j - 1];
} else {
matrix[i][j] = Math.min(matrix[i - 1][j], matrix[i][j - 1], matrix[i - 1][j - 1]) + 1;
}
}
}
return matrix[len1][len2];
};