Skip to content

Commit 665315d

Browse files
committed
Add day 8
1 parent 141e528 commit 665315d

File tree

5 files changed

+147
-0
lines changed

5 files changed

+147
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Read [CONTRIBUTING.md](./CONTRIBUTING.md) for contribution guidelines.
2424
5. [Day 5 -- Pattern Printing Problems](./day5/) -- [http://codetoexpress.tech/dc/day5/](http://codetoexpress.tech/dc/day5/)
2525
6. [Day 6 -- Sentence Cap + Word Reversal + Anagram](./day6/) -- [http://codetoexpress.tech/dc/day6/](http://codetoexpress.tech/dc/day6/)
2626
7. [Day 7 -- One Edit Away](./day7/) -- [http://codetoexpress.tech/dc/day7/](http://codetoexpress.tech/dc/day7/)
27+
8. [Day 8 -- Minimum Edit Distance](./day8/) -- [http://codetoexpress.tech/dc/day8/](http://codetoexpress.tech/dc/day8/)
2728

2829
## Timeline
2930

day7/cover.png

1.55 KB
Loading

day8/JavaScript/sol.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* @author MadhavBahlMD
3+
* @date 31/12/2018
4+
*/
5+
6+
function minEditDist (str1, str2) {
7+
// Initialize the distance matrix with all zeroes
8+
let len1 = str1.length,
9+
len2 = str2.length,
10+
distMatrix = [];
11+
12+
for (let i=0; i<=len2; i++) {
13+
let row = [];
14+
for (let j=0; j<=len1; j++) {
15+
// Initialie the edit distance matrix by all zeroes
16+
row.push(0);
17+
}
18+
distMatrix.push(row);
19+
}
20+
21+
// Fill the first row
22+
for (let i=0; i<=len1; i++) {
23+
distMatrix[0][i] = i;
24+
}
25+
26+
// Fill the first column
27+
for (let j=0; j<=len2; j++) {
28+
distMatrix[j][0] = j;
29+
}
30+
31+
// Fill the edit distance matrix
32+
for (let i=1; i<=len2; i++) {
33+
for (let j=1; j<=len1; j++) {
34+
if (str1[i-1] === str2[j-1]) {
35+
distMatrix[i][j] = distMatrix[i-1][j-1];
36+
} else {
37+
distMatrix [i][j] = 1 + Math.min (
38+
distMatrix[i-1][j-1],
39+
distMatrix[i-1][j],
40+
distMatrix[i][j-1]
41+
);
42+
}
43+
}
44+
}
45+
46+
return distMatrix[len2][len1];
47+
}
48+
49+
console.log(minEditDist ('abcdefgs', 'agced'));
50+
console.log(minEditDist('kitten', 'sitting'));

day8/README.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
![cover](./cover.png)
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 ![lev](https://wikimedia.org/api/rest_v1/media/math/render/svg/4cf357d8f2135035207088d2c7b890fb4b64e410) where,
27+
28+
![def](https://wikimedia.org/api/rest_v1/media/math/render/svg/f0a48ecfc9852c042382fdc33c19e11a16948e85)
29+
30+
where
31+
![def](https://wikimedia.org/api/rest_v1/media/math/render/svg/52512ede08444b13838c570ba4a3fc71d54dbce9)
32+
is the indicator function equal to `0` when
33+
![def](https://wikimedia.org/api/rest_v1/media/math/render/svg/231fda9ee578f0328c5ca28088d01928bb0aaaec)
34+
and equal to 1 otherwise, and
35+
![def](https://wikimedia.org/api/rest_v1/media/math/render/svg/bdc0315678caad28648aafedb6ebafb16bd1655c)
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+
```

day8/cover.png

139 KB
Loading

0 commit comments

Comments
 (0)