|
| 1 | + |
| 2 | + |
| 3 | +# Day 8 - Maximum Edit Distance (Levenshtein Distance) |
| 4 | + |
| 5 | +Given two strings, and operations like replace, delete and add, write a program to determine how many minimum operations would it take to convert one string into another string. |
| 6 | + |
| 7 | +**Hint: Use Lavenshtein Distance** |
| 8 | + |
| 9 | +## Example (source: [Wikipedia](https://en.wikipedia.org/wiki/Levenshtein_distance)) |
| 10 | + |
| 11 | +For example, the (Minimum Edit) Levenshtein distance between `kitten` and |
| 12 | +`sitting` is `3`, since the following three edits change one |
| 13 | +into the other, and there is no way to do it with fewer than |
| 14 | +three edits: |
| 15 | + |
| 16 | +1. **k**itten → **s**itten (substitution of "s" for "k") |
| 17 | +2. sitt**e**n → sitt**i**n (substitution of "i" for "e") |
| 18 | +3. sittin → sittin**g** (insertion of "g" at the end). |
| 19 | + |
| 20 | +## Levenshtein distance (source: [Wikipedia](https://en.wikipedia.org/wiki/Levenshtein_distance)) |
| 21 | + |
| 22 | +Levenshtein distance between two words is the minimum number of single-character edits (insertions, deletions or substitutions) required to change one word into the other. It is named after the Soviet mathematician Vladimir Levenshtein, who considered this distance in 1965. |
| 23 | + |
| 24 | +### Definition |
| 25 | + |
| 26 | +Mathematically, the Levenshtein distance between two strings `a` and `b` (of length `|a|` and `|b|` respectively) is given by  where, |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +where |
| 31 | + |
| 32 | +is the indicator function equal to `0` when |
| 33 | + |
| 34 | +and equal to 1 otherwise, and |
| 35 | + |
| 36 | +is the distance between the first `i` characters of `a` and the first |
| 37 | +`j` characters of `b`. |
| 38 | + |
| 39 | +Therefore, the minimum edit distance between `a` and `b` is the last element in the edit distance matrix |
| 40 | + |
| 41 | +## JavaScript Implementation |
| 42 | + |
| 43 | +### [Solution](./JavaScript/sol.js) |
| 44 | + |
| 45 | +```js |
| 46 | +/** |
| 47 | + * @author MadhavBahlMD |
| 48 | + * @date 31/12/2018 |
| 49 | + */ |
| 50 | + |
| 51 | +function minEditDist (str1, str2) { |
| 52 | + // Initialize the distance matrix with all zeroes |
| 53 | + let len1 = str1.length, |
| 54 | + len2 = str2.length, |
| 55 | + distMatrix = []; |
| 56 | + |
| 57 | + for (let i=0; i<=len2; i++) { |
| 58 | + let row = []; |
| 59 | + for (let j=0; j<=len1; j++) { |
| 60 | + // Initialie the edit distance matrix by all zeroes |
| 61 | + row.push(0); |
| 62 | + } |
| 63 | + distMatrix.push(row); |
| 64 | + } |
| 65 | + |
| 66 | + // Fill the first row |
| 67 | + for (let i=0; i<=len1; i++) { |
| 68 | + distMatrix[0][i] = i; |
| 69 | + } |
| 70 | + |
| 71 | + // Fill the first column |
| 72 | + for (let j=0; j<=len2; j++) { |
| 73 | + distMatrix[j][0] = j; |
| 74 | + } |
| 75 | + |
| 76 | + // Fill the edit distance matrix |
| 77 | + for (let i=1; i<=len2; i++) { |
| 78 | + for (let j=1; j<=len1; j++) { |
| 79 | + if (str1[i-1] === str2[j-1]) { |
| 80 | + distMatrix[i][j] = distMatrix[i-1][j-1]; |
| 81 | + } else { |
| 82 | + distMatrix [i][j] = 1 + Math.min ( |
| 83 | + distMatrix[i-1][j-1], |
| 84 | + distMatrix[i-1][j], |
| 85 | + distMatrix[i][j-1] |
| 86 | + ); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return distMatrix[len2][len1]; |
| 92 | +} |
| 93 | + |
| 94 | +console.log(minEditDist ('abcdefgs', 'agced')); |
| 95 | +console.log(minEditDist('kitten', 'sitting')); |
| 96 | +``` |
0 commit comments