|
| 1 | + |
| 2 | + |
| 3 | +# Day 7 -- One Edit Away |
| 4 | + |
| 5 | +**Question** -- Given two strings, check whether they are one edit distance apart, i.e., if str1 and str2 are the given strings, check whether str1 can be converted to str2 with exactly one edit. |
| 6 | + |
| 7 | +An edit between 2 strings is one of the following operations |
| 8 | + |
| 9 | +1. Replace a character |
| 10 | +2. Add a character |
| 11 | +3. Delete a character |
| 12 | + |
| 13 | +Also note that two same strings are also one edit away since we can say that anyone of those strings can be converted into another by just one replace operation where we replace any of the character by itself. |
| 14 | + |
| 15 | +**Example** |
| 16 | + |
| 17 | +``` |
| 18 | +Input: |
| 19 | + str1 = 'abc', str2 = 'abc' |
| 20 | +Output: yes |
| 21 | +``` |
| 22 | + |
| 23 | +``` |
| 24 | +Input: |
| 25 | + str1 = 'abc', str2 = 'abd' |
| 26 | +Output: yes |
| 27 | +``` |
| 28 | + |
| 29 | +``` |
| 30 | +Input: |
| 31 | + str1 = 'abc', str2 = 'ab' |
| 32 | +Output: yes |
| 33 | +``` |
| 34 | + |
| 35 | +``` |
| 36 | +Input: |
| 37 | + str1 = 'abc', str2 = 'abcd' |
| 38 | +Output: yes |
| 39 | +``` |
| 40 | + |
| 41 | +``` |
| 42 | +Input: |
| 43 | + str1 = 'abc', str2 = 'abdef' |
| 44 | +Output: no |
| 45 | +``` |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | +## JavaScript Implementatiom |
| 50 | + |
| 51 | +### [Solution](./JavaScript/sol.js) |
| 52 | + |
| 53 | +```js |
| 54 | +/** |
| 55 | + * @author MadhavBahlMD |
| 56 | + * @date 28/12/2018 |
| 57 | + * METHOD - Consider the 3 cases separately, |
| 58 | + * 1) If difference in lengths is more than 1 then print no and exit |
| 59 | + * 2) If the length is same and the hamming distance is 1, print yes |
| 60 | + * 3) If difference in length equals 1, then loop through the bigger string and check for the corresponding elements of smaller string |
| 61 | + */ |
| 62 | + |
| 63 | +function oneEditAway (str1, str2) { |
| 64 | + let len1 = str1.length, |
| 65 | + len2 = str2.length; |
| 66 | + |
| 67 | + if (Math.abs(len1-len2) > 1) { |
| 68 | + // If difference in lengths is greater than 1 |
| 69 | + console.log (`Strings "${str1}" and "${str2}" are not one edit away`); |
| 70 | + return 0; |
| 71 | + } else if(len1 === len2) { |
| 72 | + // if lengths are equal |
| 73 | + let count = 0; |
| 74 | + |
| 75 | + for (let i=0; i<len1; i++) { |
| 76 | + if (str1[i] !== str2[i]) count++; |
| 77 | + } |
| 78 | + |
| 79 | + if (count < 2) { // hamming distance is 0 or 1 |
| 80 | + console.log (`Strings "${str1}" and "${str2}" are one edit away`); |
| 81 | + return 1; |
| 82 | + } else { // hamming distance >= 2 |
| 83 | + console.log (`Strings "${str1}" and "${str2}" are not one edit away`); |
| 84 | + return 0; |
| 85 | + } |
| 86 | + |
| 87 | + } else { |
| 88 | + // Difference in lenghts = 1 |
| 89 | + if (len1 > len2) return checkOneEdit (str1, str2); |
| 90 | + else return checkOneEdit (str2, str1); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +function checkOneEdit (str1, str2) { |
| 95 | + let edit = 0, j=0; |
| 96 | + for (let i=0; i<str1.length; i++) { |
| 97 | + if (str1[i] !== str2[j]) { |
| 98 | + edit++; |
| 99 | + } else { |
| 100 | + j++; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + if (edit >= 2) { |
| 105 | + console.log (`Strings "${str1}" and "${str2}" are not one edit away`); |
| 106 | + return 0; |
| 107 | + } else { |
| 108 | + console.log (`Strings "${str1}" and "${str2}" are one edit away`); |
| 109 | + return 1; |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +// Test Cases |
| 114 | +oneEditAway ('abc', 'abc'); // true |
| 115 | +oneEditAway ('abc', 'abd'); // true |
| 116 | +oneEditAway ('abc', 'ab'); // true |
| 117 | +oneEditAway ("a", "a"); // true |
| 118 | +oneEditAway ("abcdef", "abqdef"); // true |
| 119 | +oneEditAway ("abcdef", "abccef"); // true |
| 120 | +oneEditAway ("abcdef", "abcde"); // true |
| 121 | +oneEditAway ("aaa", "abc"); // false |
| 122 | +oneEditAway ('bc', 'abc'); // true |
| 123 | +oneEditAway ('abc', 'abced'); // false |
| 124 | +``` |
0 commit comments