Given two numeric strings, A and B. A numeric string is a string that contains only digits [‘0’-‘9’].
The task is to make both the strings equal in minimum cost. The only operation that you are allowed to do is to delete a character (i.e. digit) from any of the strings (A or B). The cost of deleting a digit D is D units.
Input : A = “7135”, B = “135”
Output : 7
To make both string identical we have to delete ‘7’ from string A.
Input : A = “9142”, B = “1429”
Output : 14
There are 2 ways to make string “9142” identical to “1429” i.e either by deleting ‘9’ from both the strings or by deleting ‘1’, ‘4’and ‘2’ from both the string. Deleting 142 from both the string will cost 2*(1+4+2)=14 which is more optimal than deleting ‘9’.
This problem is a variation of a popular Dynamic Programming problem – Longest Common Subsequence. The idea is to find the maximum weight common subsequence which will be our required optimal identical string. To find the cost of deletion, subtract the sum of maximum weight common subsequence from the sum of string A and B.
Minimum weight to make string identical = costA + costB – 2*(cost of LCS)
Below is the implementation of the above idea:
- Minimum Cost To Make Two Strings Identical
- Minimum cost to make two strings identical by deleting the digits
- Minimum cost to make two strings same
- Minimum cost to make a string free of a subsequence
- 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 pairs required to make two strings same
- Minimum number of given operations required to make two strings equal
- Find the minimum number of preprocess moves required to make two strings equal
- Using Counter() in Python to find minimum character removal to make two strings anagram
- Minimum Number of Manipulations required to make two Strings Anagram Without Deletion of Character
- Least number of manipulations needed to ensure two strings have identical characters
- Cost to make a string Panagram
- Cost to make a string Panagram | Set 2
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.