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:
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 strings identical by deleting the digits
- Minimum cost to make two strings same
- Least number of manipulations needed to ensure two strings have identical characters
- Minimum circular rotations to obtain a given numeric string by avoiding a set of given strings
- Minimum cost to make Longest Common Subsequence of length k
- Minimum cost to make a string free of a subsequence
- 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
- 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
- Minimum cost to sort strings using reversal operations of different costs
- Minimum cost to reach a point N from 0 with two different operations allowed
- Reduce the string by removing K consecutive identical characters
- Recursive program to insert a star between pair of identical characters
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.