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:
# Python 3 program to find minimum cost
# to make two numeric strings identical
# Function to find weight of LCS
def lcs(dp, a, b, m, n):
for i in range(100):
for j in range(100):
dp[i][j] = -1
if (m < 0 or n < 0) : return 0 # if this state is already calculated # then return if (dp[m][n] != -1): return dp[m][n] ans = 0 if (a[m] == b[n]): # adding required weight for # particular match ans = (ord(a[m]) - 48) + lcs(dp, a, b, m - 1, n - 1) else: # recurse for left and right child # and store the max ans = max(lcs(dp, a, b, m - 1, n), lcs(dp, a, b, m, n - 1)) dp[m][n] = ans return ans # Function to calculate cost of string def costOfString(s): cost = 0 for i in range(len(s)): cost += (ord(s[i]) - 48) return cost # Driver code if __name__ == "__main__": a = "9142" b = "1429" dp = [[0 for x in range(101)] for y in range(101)] # Minimum cost needed to make two # strings identical print(costOfString(a) + costOfString(b) - 2 * lcs(dp, a, b, len(a) - 1, len(b) - 1)) # This code is contributed by ita_c [tabby title="C#"]
- 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 number of pairs required to make two strings same
- Minimum move to end operations to make all strings equal
- Minimum number of given operations required to make two strings equal
- Using Counter() in Python to find minimum character removal to make two strings anagram
- Find the minimum number of preprocess moves required to make two strings equal
- 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 | Set 2
- Cost to make a string Panagram
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.