Challenge: Edit Distance Problem
Let's solve the famous Edit Distance/Levenshtein Distance problem!
We'll cover the following
Edit distance is a metric to quantify how dissimilar two strings are to one another by counting the minimum number of operations required to transform one string into the other.
Edit distances find several applications in the real world. For example, it is used to figure out which word is misspelled in automatic spelling correction. In bioinformatics, it’s used to quantify the similarity between two DNA sequences.
Different definitions of an edit distance use different sets of string operations to determine the ‘distance’. The Levenshtein distance operations are the most famous: removal, insertion, or substitution of a character in the string. Levenshtein distance operations are what we will follow in this example.
Problem Statement
Given two strings, write code to calculate how many minimum Levenshtein distance operations are needed to convert one into the other.
Input
Two strings
Output
A number
Sample Input
string str1 = "sunday";
string str2 = "saturday";
int m = str1.length;
int m = str2.length;
Sample Output
3
Coding Exercise
Take a close look and design a step-by-step algorithm before jumping on to the implementation. This problem is designed for your practice, so try to solve it on your own first. If you get stuck, you can always refer to the hint and solution provided in the code tab. Good Luck!
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.