The Levenshtein distance also called the Edit distance, is the minimum number of operations required to transform one string to another.
Typically, three types of operations are performed (one at a time) :
- Replace a character.
- Delete a character.
- Insert a character.
Input: str1 = “glomax”, str2 = “folmax”
str1 is converted to str2 by replacing ‘g’ with ‘o’, deleting the second ‘o’, and inserting ‘f’ at the beginning. There is no way to do it with fewer than three edits.
Input: s1 = “GIKY”, s2 = “GEEKY”
s1 is converted to s2 by inserting ‘E’ right after ‘G’,and replacing ‘I’ with ‘E’.
This problem can be done in two ways :
- Using Recursion.
- Using Dynamic Programming.
Method 1: Recursive Approach
Let’s consider by taking an example
Given two strings s1 = “sunday” and s2 = “saturday”. We want to convert “sunday” into “saturday” with minimum edits.
- Consider ‘i’ and ‘j’ as the upper-limit indices of substrings generated using s1 and s2.
- Let us pick i = 2 and j = 4 i.e. prefix strings are ‘su’ and ‘satu’ respectively (assume the strings indices start at 1). The rightmost characters can be aligned in three different possible ways.
- Possible Case 1: Align the characters ‘u’ and ‘u’. They are equal, no edit is required. We still left with the problem of i = 1 and j = 3, so we should proceed to find Levenshtein distance(i-1, j-1).
- Possible Case 2 (Deletion): Align the right character from the first string and no character from the second string. We need a deletion here. We still left with problem of i = 1 and j = 4, so we should proceed finding Levenshtein distance(i-1, j).
- Possible Case 3 (Insertion): Align the right character from the second string and no character from the first string. We need an insertion here. We still left with the problem of i = 2 and j = 3, so we should proceed to find Levenshtein distance(i, j-1).
- We assume that the character to be inserted in the first string is the same as the right character of the second string.
- Possible Case 4 (Replacement): Align right characters from the first string as well as from the second string. We need a substitution here. We still left with problem of i = 1 and j = 3, so we should proceed finding Levenshtein distance(i-1, j-1).
- We assume that the replaced character in the first string is the same as the right character of the second string.
- We have to find the minimum of all the possible three cases.
Time Complexity: O(3^n) because at each step, we branch-off into three recursive calls. Here, ‘n’ is the length of the first string.
Method 2: Dynamic Programming Approach
If we draw the recursion tree of the above solution, we can see that the same sub-problems are getting computed again and again. We know that Dynamic Programming comes to the picture when subproblem solutions can be memoized rather than computed again and again.
- The Memoized version follows the top-down approach since we first break the problems into subproblems and then calculate and store values.
- We can also solve this problem in a bottom-up approach. In a bottom-up manner, we solve the sub-problems first, then solve larger sub-problems from them.
Dynamic Programming Implementation (Optimised approach)
Time Complexity: O(m*n), where m is the length of the first string, and n is the length of the second string.
Auxiliary Space: O(m*n), as the matrix used in the above implementation has dimensions m*n.
- Spell Checkers.
- Speech Recognition.
- DNA Analysis.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.