Given two strings ‘str1’ and ‘str2’ of size m and n respectively. The task is to remove/delete and insert the minimum number of characters from/in str1 to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Input : str1 = "heap", str2 = "pea" Output : Minimum Deletion = 2 and Minimum Insertion = 1 Explanation: p and h deleted from heap Then, p is inserted at the beginning One thing to note, though p was required yet it was removed/deleted first from its position and then it is inserted to some other position. Thus, p contributes one to the deletion_count and one to the insertion_count.
Input : str1 = "geeksforgeeks", str2 = "geeks" Output : Minimum Deletion = 8 Minimum Insertion = 0
A simple approach is to consider all subsequences of str1 and for each subsequence calculate minimum deletions and insertions so as to transform it into str2. A very complex method and the time complexity of this solution is exponential.
An efficient approach uses the concept of finding the length of the longest common subsequence of the given two sequences.
- str1 and str2 be the given strings.
- m and n be their lengths respectively.
- len be the length of the longest common subsequence of str1 and str2
- minimum number of deletions minDel = m – len
- minimum number of Insertions minInsert = n – len
Below is the implementation of the above code:
Minimum number of deletions = 2 Minimum number of insertions = 1
Time Complexity: O(m * n)
This article is contributed by Ayush Jauhari. 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 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.
- Check if it is possible to transform one string to another
- Minimum operations to transform given string to another by moving characters to front or end
- Minimum number of deletions to make a string palindrome | Set 2
- Minimum number of deletions to make a string palindrome
- Print all possible ways to convert one string into another string | Edit-Distance
- Minimum number of deletions to make a sorted sequence
- Minimum insertions to sort an array
- Minimum insertions to form a palindrome | DP-28
- Split the string into minimum parts such that each part is in the another string
- Find length of longest subsequence of one string which is substring of another string
- Count of arrays in which all adjacent elements are such that one of them divide the another
- Check if end of a sorted Array can be reached by repeated jumps of one more, one less or same number of indices as previous jump
- Minimum cost required to rearrange a given array to make it equal to another given array
- Minimum splits required to convert a number into prime segments
- Paper Cut into Minimum Number of Squares | Set 2
- Split array into minimum number of subsets having maximum pair sum at most K
- Split a given array into K subarrays minimizing the difference between their maximum and minimum
- Number of ways to partition a string into two balanced subsequences
- Optimally accommodate 0s and 1s from a Binary String into K buckets
- Split a binary string into K subsets minimizing sum of products of occurrences of 0 and 1