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)
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.
- Minimum Cost To Make Two Strings Identical
- Minimum Cost to make two Numeric Strings Identical
- Minimum digits to be removed to make either all digits or alternating digits same
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Minimum cost to make two strings same
- Least number of manipulations needed to ensure two strings have identical characters
- Append digits to the end of dupicate strings to make all strings in an array unique
- Minimum steps to delete a string by deleting substring comprising of same characters
- Minimum cost to make Longest Common Subsequence of length k
- Minimum cost to make a string free of a subsequence
- Minimum cost required to rearrange a given array to make it equal to another given array
- Minimum Number of Manipulations required to make two Strings Anagram Without Deletion of Character
- Minimum number of given operations required to make two strings equal
- Find the minimum number of preprocess moves required to make two strings equal
- Minimum number of pairs required to make two strings same
- Minimum swaps to make two strings equal by swapping only with third string
- Minimum number of operations required to make two strings equal
- Minimum characters to be deleted from the beginning of two strings to make them equal
- Minimum characters to be deleted from the end to make given two strings equal
- Using Counter() in Python to find minimum character removal to make two strings anagram
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.