In standard Edit Distance where we are allowed 3 operations, insert, delete and replace. Consider a variation of edit distance where we are allowed only two operations insert and delete, find edit distance in this variation.
Input : str1 = "cat", st2 = "cut" Output : 2 We are allowed to insert and delete. We delete 'a' from "cat" and insert "u" to make it "cut". Input : str1 = "acb", st2 = "ab" Output : 1 We can convert "acb" to "ab" by removing 'c'.
One solution is to simply modify Edit Distance Solution by making two recursive call instead of three. An interesting solution is based on LCS.
1) Find LCS of two strings. Let length of LCS be x.
2) Let length of first string be m and length of second string be n. Our result is (m – x) + (n – x). We basically need to do (m – x) delete operations and (n – x) insert operations.
Time Complexity : O(m * n)
Auxiliary Space : O(m * n)
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.
- Longest Increasing Subsequence using Longest Common Subsequence Algorithm
- Longest Common Subsequence | DP-4
- Printing Longest Common Subsequence
- C++ Program for Longest Common Subsequence
- Longest Common Increasing Subsequence (LCS + LIS)
- Longest Common Subsequence | DP using Memoization
- Longest Common Subsequence with at most k changes allowed
- Longest Common Anagram Subsequence
- LCS (Longest Common Subsequence) of three strings
- Longest common subsequence with permutations allowed
- Length of longest common subsequence containing vowels
- Java Program for Longest Common Subsequence
- Python Program for Longest Common Subsequence
- Longest common anagram subsequence from N strings
- Longest subsequence such that adjacent elements have at least one common digit
- Minimum cost to make Longest Common Subsequence of length k
- Edit Distance | DP-5
- Printing Longest Common Subsequence | Set 2 (Printing All)
- Edit Distance | DP using Memoization
- Check if edit distance between two strings is one
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.