Minimum cost to make two strings identical by deleting the digits

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

C++

/* C++ code to find minimum cost to make two strings
   identical */
#include <bits/stdc++.h>
using namespace std;

/* Function to returns cost of removing the identical
   characters in LCS for X[0..m-1], Y[0..n-1] */
int lcs(char* X, char* Y, int m, int n)
{
    int L[m + 1][n + 1];

    /* Following steps build L[m+1][n+1] in bottom
       up fashion. Note that L[i][j] contains cost
       of removing identical characters in LCS of
       X[0..i-1] and Y[0..j-1] */
    for (int i = 0; i <= m; ++i) {
        for (int j = 0; j <= n; j++) {

            if (i == 0 || j == 0)
                L[i][j] = 0;

            // If both characters are same, add both
            // of them
            else if (X[i - 1] == Y[j - 1])
                L[i][j] = L[i - 1][j - 1] + 
                          2 * (X[i - 1] - '0');

            // Otherwise find the maximum cost among them
            else
                L[i][j] = max(L[i - 1][j], L[i][j - 1]);
        }
    }

    return L[m][n];
}

// Returns cost of making X[] and Y[] identical
int findMinCost(char X[], char Y[])
{
    // Find LCS of X[] and Y[]
    int m = strlen(X), n = strlen(Y);

    // Initialize the cost variable
    int cost = 0;

    // Find cost of all characters in
    // both strings
    for (int i = 0; i < m; ++i)
        cost += X[i] - '0';

    for (int i = 0; i < n; ++i)
        cost += Y[i] - '0';

    return cost - lcs(X, Y, m, n);
}

/* Driver program to test above function */
int main()
{
    char X[] = "3759";
    char Y[] = "9350";
    cout << "Minimum Cost to make two strings "
         << "identical is = " << findMinCost(X, Y);
    return 0;
}

Java

// Java code to find minimum cost to 
// make two strings identical
import java.util.*;
import java.lang.*;

public class GfG{
    
/* Function to returns cost of removing the identical
characters in LCS for X[0..m-1], Y[0..n-1] */
static int lcs(char[] X, char[] Y, int m, int n)
{
    int[][] L=new int[m + 1][n + 1];

    /* Following steps build L[m+1][n+1] in 
    bottom up fashion. Note that L[i][j] contains 
    cost of removing identical characters in 
    LCS of X[0..i-1] and Y[0..j-1] */
    for (int i = 0; i <= m; ++i) {
        for (int j = 0; j <= n; j++) {

            if (i == 0 || j == 0)
                L[i][j] = 0;

            // If both characters are same, 
            // add both of them
            else if (X[i - 1] == Y[j - 1])
                L[i][j] = L[i - 1][j - 1] + 
                        2 * (X[i - 1] - '0');

            // Otherwise find the maximum 
            // cost among them
            else
                L[i][j] = L[i - 1][j] > L[i][j - 1] ? 
                          L[i - 1][j] : L[i][j - 1];
        }
    }

    return L[m][n];
}

// Returns cost of making X[] and Y[] identical
static int findMinCost(char X[], char Y[])
{
    // Find LCS of X[] and Y[]
    int m = X.length, n = Y.length;

    // Initialize the cost variable
    int cost = 0;

    // Find cost of all characters in
    // both strings
    for (int i = 0; i < m; ++i)
        cost += X[i] - '0';

    for (int i = 0; i < n; ++i)
        cost += Y[i] - '0';

    return cost - lcs(X, Y, m, n);
}

// driver function
    public static void main(String argc[]){

    char X[] = ("3759").toCharArray();
    char Y[] = ("9350").toCharArray();
    
    System.out.println("Minimum Cost to make two strings"+
                 " identical is = " +findMinCost(X, Y));
    }
}

// This code is contributed by Prerna Saini

C#


// C# code to find minimum cost to 
// make two strings identical
using System;

public class GfG{
    
    /* Function to returns cost of removing the identical
    characters in LCS for X[0..m-1], Y[0..n-1] */
    static int lcs(string X, string Y, int m, int n)
    {
        int [,]L=new int[m + 1,n + 1];
    
        /* Following steps build L[m+1][n+1] in 
        bottom up fashion. Note that L[i][j] contains 
        cost of removing identical characters in 
        LCS of X[0..i-1] and Y[0..j-1] */
        for (int i = 0; i <= m; ++i) {
            for (int j = 0; j <= n; j++) {
    
                if (i == 0 || j == 0)
                    L[i,j] = 0;
    
                // If both characters are same, 
                // add both of them
                else if (X[i - 1] == Y[j - 1])
                    L[i,j] = L[i - 1,j - 1] + 
                            2 * (X[i - 1] - '0');
    
                // Otherwise find the maximum 
                // cost among them
                else
                    L[i,j] = L[i - 1,j] > L[i,j - 1] ? 
                            L[i - 1,j] : L[i,j - 1];
            }
        }
    
        return L[m,n];
    }
    
    // Returns cost of making X[] and Y[] identical
    static int findMinCost( string X, string Y)
    {
        // Find LCS of X[] and Y[]
        int m = X.Length, n = Y.Length;
    
        // Initialize the cost variable
        int cost = 0;
    
        // Find cost of all characters in
        // both strings
        for (int i = 0; i < m; ++i)
            cost += X[i] - '0';
    
        for (int i = 0; i < n; ++i)
            cost += Y[i] - '0';
    
        return cost - lcs(X, Y, m, n);
    }
    
    // Driver function
    public static void Main()
    {
        string X = "3759";
        string Y= "9350";
        
        Console.WriteLine("Minimum Cost to make two strings"+
                    " identical is = " +findMinCost(X, Y));
    }
}

// This code is contributed by vt_m


Output:

Minimum Cost to make two strings  identical is = 23

Time complexity: O(m*n)
Auxiliary space: O(m*n)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.




Article Tags :
Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



3.3 Average Difficulty : 3.3/5.0
Based on 3 vote(s)






User Actions