Given two strings X and Y, and two values costX and costY. We need to find minimum cost required to make the given two strings identical. We can delete characters from both the strings. The cost of deleting a character from string X is costX and from Y is costY. Cost of removing all characters from a string is same.
Input : X = "abcd", Y = "acdb", costX = 10, costY = 20. Output: 30 For Making both strings identical we have to delete character 'b' from both the string, hence cost will be = 10 + 20 = 30. Input : X = "ef", Y = "gh", costX = 10, costY = 20. Output: 60 For making both strings identical, we have to delete 2-2 characters from both the strings, hence cost will be = 10 + 10 + 20 + 20 = 60.
This problem is a variation of Longest Common Subsequence ( LCS ). The idea is simple, we first find the length of longest common subsequence of strings X and Y. Now subtracting len_LCS with lengths of individual strings gives us number of characters to be removed to make them identical.
// Cost of making two strings identical is SUM of following two // 1) Cost of removing extra characters (other than LCS) // from X // 2) Cost of removing extra characters (other than LCS) // from Y Minimum Cost to make strings identical = costX * (m - len_LCS) + costY * (n - len_LCS). m ==> Length of string X m ==> Length of string Y len_LCS ==> Length of LCS Of X and Y. costX ==> Cost of removing a character from X costY ==> Cost of removing a character from Y Note that cost of removing all characters from a string is same.
Below is the implementation of above idea.
Minimum Cost to make two strings identical is = 60
This article is contributed by Shashank Mishra ( Gullu ). This article is reviwed by team geeksforgeeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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 by deleting the digits
- Minimum Cost to make two Numeric Strings Identical
- Minimum cost to make two strings same
- Least number of manipulations needed to ensure two strings have identical characters
- 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
- Reduce the string by removing K consecutive identical characters
- Recursive program to insert a star between pair of identical characters
- Check three or more consecutive identical characters or numbers
- Minimum cost to reach a point N from 0 with two different operations allowed