forked from CodeToExpress/dailycodebase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sol.js
70 lines (62 loc) · 2.22 KB
/
sol.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
/**
* @author MadhavBahlMD
* @date 28/12/2018
* METHOD - Consider the 3 cases separately,
* 1) If difference in lengths is more than 1 then print no and exit
* 2) If the length is same and the hamming distance is 1, print yes
* 3) If difference in length equals 1, then loop through the bigger string and check for the corresponding elements of smaller string
*/
function oneEditAway (str1, str2) {
let len1 = str1.length,
len2 = str2.length;
if (Math.abs(len1-len2) > 1) {
// If difference in lengths is greater than 1
console.log (`Strings "${str1}" and "${str2}" are not one edit away`);
return 0;
} else if(len1 === len2) {
// if lengths are equal
let count = 0;
for (let i=0; i<len1; i++) {
if (str1[i] !== str2[i]) count++;
}
if (count < 2) { // hamming distance is 0 or 1
console.log (`Strings "${str1}" and "${str2}" are one edit away`);
return 1;
} else { // hamming distance >= 2
console.log (`Strings "${str1}" and "${str2}" are not one edit away`);
return 0;
}
} else {
// Difference in lenghts = 1
if (len1 > len2) return checkOneEdit (str1, str2);
else return checkOneEdit (str2, str1);
}
}
function checkOneEdit (str1, str2) {
let edit = 0, j=0;
for (let i=0; i<str1.length; i++) {
if (str1[i] !== str2[j]) {
edit++;
} else {
j++;
}
}
if (edit >= 2) {
console.log (`Strings "${str1}" and "${str2}" are not one edit away`);
return 0;
} else {
console.log (`Strings "${str1}" and "${str2}" are one edit away`);
return 1;
}
}
// Test Cases
oneEditAway ('abc', 'abc'); // true
oneEditAway ('abc', 'abd'); // true
oneEditAway ('abc', 'ab'); // true
oneEditAway ("a", "a"); // true
oneEditAway ("abcdef", "abqdef"); // true
oneEditAway ("abcdef", "abccef"); // true
oneEditAway ("abcdef", "abcde"); // true
oneEditAway ("aaa", "abc"); // false
oneEditAway ('bc', 'abc'); // true
oneEditAway ('abc', 'abced'); // false