Levenshtein Distance - Coderust_ Hacking the Coding Interview
Levenshtein Distance - Coderust_ Hacking the Coding Interview
Levenshtein Distance
Compute the Levenshtein distance between two strings.
• Description
• Hints
• Try it yourself
• Solution
• Runtime complexity
• Memory complexity
• Solution 3: iterative (optimized)
• Runtime complexity
• Memory complexity
Description #
Suppose we have coin denominations of [1, 2, 5] and the total amount is 7.
We can make changes in the following 6 ways:
https://www.educative.io/courses/coderust-hacking-the-coding-interview/j2A0v 1/10
02/11/2020 Levenshtein Distance - Coderust: Hacking the Coding Interview
Denominations 1, 2, 5
Amount 7
Total Methods 6
Hints #
Use principles of dynamic programming.
Try it yourself #
C++ Java Python JS Ruby
Solution #
Runtime complexity #
The runtime complexity of this solution is quadratic, O(m × n), where m
is the number of denominations and n is the total amount.
Memory complexity #
https://www.educative.io/courses/coderust-hacking-the-coding-interview/j2A0v 2/10
02/11/2020 Levenshtein Distance - Coderust: Hacking the Coding Interview
The coin changing problem has both optimal substructure, meaning that it
can be easily divided to simpler problems and they can be solved to find
the final solution. It also satisfies the property of overlapping sub
problems, meaning previously solved sub problem results can be used
multiple times.
There is only one way you can make a change of 0 , i.e., select no coin so
we’ll initialize solution[0] = 1 . We’ll solve the problem for each
amount, denomination to amount, using coins up to a denomination, den .
The solution for amount x using a denomination den will then be:
We’ll repeat this process for all the denominations, and at the last element
of the solution array, we will have the solution.
https://www.educative.io/courses/coderust-hacking-the-coding-interview/j2A0v 3/10
02/11/2020 Levenshtein Distance - Coderust: Hacking the Coding Interview
At the end, we’ll use third denomination, i.e. 5 , in our example. We’ll
compute solution[x] = solution[x] + solution[x - 5] for all 5
<= x <= amount .
0 0 0 0 0 0 0 0
1 of 19
https://www.educative.io/courses/coderust-hacking-the-coding-interview/j2A0v 4/10
02/11/2020 Levenshtein Distance - Coderust: Hacking the Coding Interview
return solution.back();
}
int main() {
vector<int> denominations = {1, 2, 5};
int amount = 7;
int result = solve_coin_change(denominations, amount);
// printing result
cout<< "solve_coin_change_dp([";
for (int den: denominations){
cout << den << " ";
}
cout << "], " << amount << ") = " << result;
}
https://www.educative.io/courses/coderust-hacking-the-coding-interview/j2A0v 5/10
02/11/2020 Levenshtein Distance - Coderust: Hacking the Coding Interview
if (str1.length() == 0)
return str2.length();
if (str2.length() == 0)
return str1.length();
// for all i and j, d[i,j] will hold the Levenshtein distance between
// the first i characters of str1 and the first j characters of str2;
// note that d has (m+1)*(n+1) values
int d[str1.length() + 1][str2.length() + 1];
int cost;
for (int i = 1; i <= str1.length(); i++) {
for (int j = 1; j <= str2.length(); j++) {
d[i][j] = minimum(
d[i - 1][j] + 1, // a deletion
d[i][j - 1] + 1, // an insertion
d[i - 1][j - 1] + cost); // a substitution
}
}
return d[str1.length()][str2.length()];
}
int main() {
https://www.educative.io/courses/coderust-hacking-the-coding-interview/j2A0v 6/10
02/11/2020 Levenshtein Distance - Coderust: Hacking the Coding Interview
cout << "LD(" << str1 << ", " << str2 << ") = " << ld;
}
Memory complexity #
The runtime complexity of this solution is linear, O(n), where m and n
are lengths of the input strings.
The optimized iterative algorithm with two rows for strings s1 = kitten
and s2 = sitting is as follows:
https://www.educative.io/courses/coderust-hacking-the-coding-interview/j2A0v 7/10
02/11/2020 Levenshtein Distance - Coderust: Hacking the Coding Interview
if s1 is equal to s2,
return 0
set m as length of s1
set n as length of s2
if s1 is empty,
return n
if s2 is empty,
return m
return d2[n]
s1 = kitten, m=6
s2 = sitting, n=7 0 1 2 3 4 5 6 7
d1
Create two arrays
d1[] and d2[] of
length n+1,
n=7 in this case 0 1 2 3 4 5 6 7
d2
1 of 13
https://www.educative.io/courses/coderust-hacking-the-coding-interview/j2A0v 8/10
02/11/2020 Levenshtein Distance - Coderust: Hacking the Coding Interview
https://www.educative.io/courses/coderust-hacking-the-coding-interview/j2A0v 9/10
02/11/2020 Levenshtein Distance - Coderust: Hacking the Coding Interview
if (str1.length() == 0)
return str2.length();
if (str2.length() == 0)
return str1.length();
int cost;
for (int i = 0; i < str1.length(); i++) {
if (str1.at(i) == str2.at(j))
cost = 0;
else
https://www.educative.io/courses/coderust-hacking-the-coding-interview/j2A0v 10/10