Skip to content

Commit 45f3739

Browse files
committed
Add Day 7 - One Edit Distance
1 parent 5462d07 commit 45f3739

File tree

4 files changed

+194
-0
lines changed

4 files changed

+194
-0
lines changed

day7/Daily Codes.png

133 KB
Loading

day7/JavaScript/sol.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* @author MadhavBahlMD
3+
* @date 28/12/2018
4+
* METHOD - Consider the 3 cases separately,
5+
* 1) If difference in lengths is more than 1 then print no and exit
6+
* 2) If the length is same and the hamming distance is 1, print yes
7+
* 3) If difference in length equals 1, then loop through the bigger string and check for the corresponding elements of smaller string
8+
*/
9+
10+
function oneEditAway (str1, str2) {
11+
let len1 = str1.length,
12+
len2 = str2.length;
13+
14+
if (Math.abs(len1-len2) > 1) {
15+
// If difference in lengths is greater than 1
16+
console.log (`Strings "${str1}" and "${str2}" are not one edit away`);
17+
return 0;
18+
} else if(len1 === len2) {
19+
// if lengths are equal
20+
let count = 0;
21+
22+
for (let i=0; i<len1; i++) {
23+
if (str1[i] !== str2[i]) count++;
24+
}
25+
26+
if (count < 2) { // hamming distance is 0 or 1
27+
console.log (`Strings "${str1}" and "${str2}" are one edit away`);
28+
return 1;
29+
} else { // hamming distance >= 2
30+
console.log (`Strings "${str1}" and "${str2}" are not one edit away`);
31+
return 0;
32+
}
33+
34+
} else {
35+
// Difference in lenghts = 1
36+
if (len1 > len2) return checkOneEdit (str1, str2);
37+
else return checkOneEdit (str2, str1);
38+
}
39+
}
40+
41+
function checkOneEdit (str1, str2) {
42+
let edit = 0, j=0;
43+
for (let i=0; i<str1.length; i++) {
44+
if (str1[i] !== str2[j]) {
45+
edit++;
46+
} else {
47+
j++;
48+
}
49+
}
50+
51+
if (edit >= 2) {
52+
console.log (`Strings "${str1}" and "${str2}" are not one edit away`);
53+
return 0;
54+
} else {
55+
console.log (`Strings "${str1}" and "${str2}" are one edit away`);
56+
return 1;
57+
}
58+
}
59+
60+
// Test Cases
61+
oneEditAway ('abc', 'abc'); // true
62+
oneEditAway ('abc', 'abd'); // true
63+
oneEditAway ('abc', 'ab'); // true
64+
oneEditAway ("a", "a"); // true
65+
oneEditAway ("abcdef", "abqdef"); // true
66+
oneEditAway ("abcdef", "abccef"); // true
67+
oneEditAway ("abcdef", "abcde"); // true
68+
oneEditAway ("aaa", "abc"); // false
69+
oneEditAway ('bc', 'abc'); // true
70+
oneEditAway ('abc', 'abced'); // false

day7/README.md

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
![cover](./cover.png)
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+
![ques](./ques.png)
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+
```

day7/ques.png

985 KB
Loading

0 commit comments

Comments
 (0)