Given two strings str1 and str2 and below operations that can performed on str1. Find minimum number of edits (operations) required to convert ‘str1’ into ‘str2’.
All of the above operations are of equal cost.
Input: str1 = "geek", str2 = "gesek"
We can convert str1 into str2 by inserting a 's'.
Input: str1 = "cat", str2 = "cut"
We can convert str1 into str2 by replacing 'a' with 'u'.
Input: str1 = "sunday", str2 = "saturday"
Last three and first characters are same. We basically
need to convert "un" to "atur". This can be done using
below three operations.
Replace 'n' with 'r', insert t, insert a
What are the subproblems in this case? The idea is process all characters one by one staring from either from left or right sides of both strings. Let us traverse from right corner, there are two possibilities for every pair of character being traversed.
m: Length of str1 (first string)
n: Length of str2 (second string)
If last characters of two strings are same, nothing much to do. Ignore last characters and get count for remaining strings. So we recur for lengths m-1 and n-1.
Else (If last characters are not same), we consider all operations on ‘str1’, consider all three operations on last character of first string, recursively compute minimum cost for all three operations and take minimum of three values.
Insert: Recur for m and n-1
Remove: Recur for m-1 and n
Replace: Recur for m-1 and n-1
Below is implementation of above Naive recursive solution.
// A Naive recursive C++ program to find minimum number
// operations to convert str1 to str2
// Utility function to find minimum of three numbers
// If first string is empty, the only option is to
// insert all characters of second string into first
if(m == 0)
// If second string is empty, the only option is to
// remove all characters of first string
if(n == 0)
// If last characters of two strings are same,
// nothing much to do. Ignore last characters and
// get count for remaining strings.
if(str1[m - 1] == str2[n - 1])
returneditDist(str1, str2, m - 1, n - 1);
// If last characters are not same, consider all
// three operations on last character of first
// string, recursively compute minimum cost for all
// three operations and take minimum of three
+ min(editDist(str1, str2, m, n - 1), // Insert
editDist(str1, str2, m - 1, n), // Remove
editDist(str1, str2, m - 1,
n - 1) // Replace
// Driver code
String str1 = "sunday";
String str2 = "saturday";
editDist(str1, str2, str1.Length, str2.Length));
// This Code is Contributed by Sam007
// A Naive recursive Python program
// to find minimum number operations
// to convert str1 to str2
// If first string is empty,
// the only option is to insert.
// all characters of second
// string into first
// If second string is empty,
// the only option is to
// remove all characters of
// first string
// If last characters of two
// strings are same, nothing
// much to do. Ignore last
// characters and get count
// for remaining strings.
if($str1[$m- 1] == $str2[$n- 1])
$m- 1, $n- 1);
// If last characters are not same,
// consider all three operations on
// last character of first string,
// recursively compute minimum cost
// for all three operations and take
// minimum of three values.
return1 + min(editDistance($str1, $str2,
$m, $n- 1), // Insert
$m- 1, $n), // Remove
$m- 1, $n- 1)); // Replace
// Driver Code
echoeditDistance($str1, $str2, strlen($str1),
// This code is contributed
// by Shivi_Aggarwal
The time complexity of above solution is exponential. In worst case, we may end up doing O(3m) operations. The worst case happens when none of characters of two strings match. Below is a recursive call diagram for worst case.
We can see that many subproblems are solved, again and again, for example, eD(2, 2) is called three times. Since same suproblems are called again, this problem has Overlapping Subprolems property. So Edit Distance problem has both properties (see this and this) of a dynamic programming problem. Like other typical Dynamic Programming(DP) problems, recomputations of same subproblems can be avoided by constructing a temporary array that stores results of subproblems.
// A Dynamic Programming based C++ program to find minimum
// number operations to convert str1 to str2
// Utility function to find the minimum of three numbers
Time Complexity: O(m x n) Auxiliary Space: O(m x n)
Space Complex Solution: In the above-given method we require O(m x n) space. This will not be suitable if the length of strings is greater than 2000 as it can only create 2D array of 2000 x 2000. To fill a row in DP array we require only one row the upper row. For example, if we are filling the i = 10 rows in DP array we require only values of 9th row. So we simply create a DP array of 2 x str1 length. This approach reduces the space complexity. Here is the C++ implementation of the above-mentioned problem
Applications: There are many practical applications of edit distance algorithm, refer Lucene API for sample. Another example, display all the words in a dictionary that are near proximity to a given wordincorrectly spelled word.
Thanks to Vivek Kumar for suggesting updates. Thanks to Venki for providing initial post. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.