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
Minimum Cost to make two strings identical is = 23
Time complexity: O(m*n)
Auxiliary space: O(m*n)
- Find maximum sum array of length less than or equal to m
- Longest palindrome subsequence with O(n) space
- Minimum cost to make Longest Common Subsequence of length k
- Maximum size subset with given sum
- Number of n digit stepping numbers
- Convert string to char array in C++
- Minimum cells required to reach destination with jumps equal to cell values
- Minimum Cost To Make Two Strings Identical
- Minimum time to finish tasks without skipping two consecutive
- Transform One String to Another using Minimum Number of Given Operation
- Minimum number of squares whose sum equals to given number n
- Maximum sum rectangle in a 2D matrix | DP-27
- Longest Bitonic Subsequence | DP-15
- Coin Change | DP-7
- Longest Common Subsequence | DP-4
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.