|
| 1 | +/* |
| 2 | +Edit Distance |
| 3 | +https://leetcode.com/problems/edit-distance/ |
| 4 | +
|
| 5 | +Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2. |
| 6 | +
|
| 7 | +You have the following 3 operations permitted on a word: |
| 8 | +
|
| 9 | +Insert a character |
| 10 | +Delete a character |
| 11 | +Replace a character |
| 12 | +Example 1: |
| 13 | +
|
| 14 | +Input: word1 = "horse", word2 = "ros" |
| 15 | +Output: 3 |
| 16 | +Explanation: |
| 17 | +horse -> rorse (replace 'h' with 'r') |
| 18 | +rorse -> rose (remove 'r') |
| 19 | +rose -> ros (remove 'e') |
| 20 | +Example 2: |
| 21 | +
|
| 22 | +Input: word1 = "intention", word2 = "execution" |
| 23 | +Output: 5 |
| 24 | +Explanation: |
| 25 | +intention -> inention (remove 't') |
| 26 | +inention -> enention (replace 'i' with 'e') |
| 27 | +enention -> exention (replace 'n' with 'x') |
| 28 | +exention -> exection (replace 'n' with 'c') |
| 29 | +exection -> execution (insert 'u') |
| 30 | +
|
| 31 | +*/ |
| 32 | + |
| 33 | +// Optimal solution |
| 34 | +var minDistance = function(word1, word2) { |
| 35 | + var matrix = []; |
| 36 | + for(var i = 0; i <= word1.length; i++) { |
| 37 | + matrix[i] = []; |
| 38 | + for(var j = 0; j <= word2.length; j++) { |
| 39 | + if(i === 0) { |
| 40 | + matrix[i][j] = j; |
| 41 | + } else if(j === 0){ |
| 42 | + matrix[i][j] = i; |
| 43 | + } else { |
| 44 | + matrix[i][j] = 0; |
| 45 | + } |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + for(var i = 1; i <= word1.length; i++) { |
| 50 | + for(var j = 1; j <= word2.length; j++) { |
| 51 | + if(word1.charAt(i - 1) === word2.charAt(j - 1)) { |
| 52 | + matrix[i][j] = matrix[i - 1][j - 1]; |
| 53 | + } else { |
| 54 | + matrix[i][j] = 1 + min( |
| 55 | + matrix[i - 1][j - 1], |
| 56 | + matrix[i - 1][j], // add |
| 57 | + matrix[i][j - 1] // remove |
| 58 | + ); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return matrix[word1.length][word2.length]; |
| 64 | +}; |
| 65 | + |
| 66 | +var min = function(a, b, c) { |
| 67 | + if(a < b) { |
| 68 | + return (a < c) ? a : c; |
| 69 | + } |
| 70 | + return (b < c) ? b : c; |
| 71 | +} |
| 72 | + |
| 73 | +//Solution 2 |
| 74 | +var minDistance2 = function(word1, word2) { |
| 75 | + return minDistanceAux(word1, word2, 0, 0); |
| 76 | +} |
| 77 | + |
| 78 | +var minDistanceAux = function(word1, word2, iter1, iter2) { |
| 79 | + if(word1.length === iter1) { |
| 80 | + return word2.length - iter2; |
| 81 | + } |
| 82 | + |
| 83 | + if(word2.length === iter2) { |
| 84 | + return word1.length - iter1; |
| 85 | + } |
| 86 | + |
| 87 | + if(word1.charAt(iter1) === word2.charAt(iter2)) { |
| 88 | + return minDistanceAux(word1, word2, iter1 + 1, iter2 + 1); |
| 89 | + } |
| 90 | + |
| 91 | + return 1 + min( |
| 92 | + minDistanceAux(word1, word2, iter1 + 1, iter2 + 1), |
| 93 | + minDistanceAux(word1, word2, iter1, iter2 + 1), // add |
| 94 | + minDistanceAux(word1, word2, iter1 + 1, iter2 ) // delete |
| 95 | + ) |
| 96 | +}; |
| 97 | + |
| 98 | +var min = function(a, b, c) { |
| 99 | + if(a < b) { |
| 100 | + return (a < c) ? a : c; |
| 101 | + } |
| 102 | + return (b < c) ? b : c; |
| 103 | +} |
| 104 | + |
| 105 | +var main = function() { |
| 106 | + console.log("-------------"); |
| 107 | + console.log("Approach 1"); |
| 108 | + console.log(minDistance("ros", "horse")); |
| 109 | + console.log(minDistance("intention", "execution")); |
| 110 | + console.log("-------------"); |
| 111 | + console.log("Approach 2"); |
| 112 | + console.log(minDistance2("ros", "horse")); |
| 113 | + console.log(minDistance2("intention", "execution")); |
| 114 | +} |
| 115 | + |
| 116 | +main(); |
| 117 | + |
| 118 | +module.exports.main = main; |
0 commit comments