Given two strings X and Y consisting of only digits ‘0’ to ‘9’. Find minimum cost required to make the given two strings identical. Only operation allowed is to delete characters from any of the string. The cost of operation of deleting the digit ‘d’ is d units.
Input: X = 3759, Y = 9350 Output: 23 Explanation For making both string identical, delete characters 3, 7, 5 from first string and delete characters 3, 5, 0 from second string. Total cost of operation is 3 + 7 + 5 + 3 + 5 + 0 = 23 Input: X = 3198, Y = 98 Output: 4
This problem is a variation of Longest Common Subsequence( LCS ) and this one. The idea is simple, instead of finding the length of longest common subsequence, find the maximum cost by adding identical characters from both the string.
Now to find the minimum cost, subtract the above result from total cost of both strings i.e.,
costX = Cost of removing all characters from string 'X' CostY = Cost of removing all characters from string 'Y' cost_Id = Cost of removing identical characters from both strings Minimum cost to make both string identical = costX + costY - cost_Id
Below is the implementation of above approach:
Minimum Cost to make two strings identical is = 23
Time complexity: O(m*n)
Auxiliary space: O(m*n)
- Minimum Cost To Make Two Strings Identical
- Minimum Cost to make two Numeric Strings Identical
- Minimum cost to make Longest Common Subsequence of length k
- Minimum cost to sort strings using reversal operations of different costs
- Minimum move to end operations to make all strings equal
- Minimum number of given operations required to make two strings equal
- Minimum Number of Manipulations required to make two Strings Anagram Without Deletion of Character
- Using Counter() in Python to find minimum character removal to make two strings anagram
- Least number of manipulations needed to ensure two strings have identical characters
- Minimum Cost Polygon Triangulation
- Minimum odd cost path in a matrix
- Minimum cost to fill given weight in a bag
- Minimum cost to construct a string
- Find minimum adjustment cost of an array
- Minimum cost to convert string into palindrome
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.
Improved By : sahilshelangia