Minimum cost to make two strings same

Given four integers a, b, c, d and two strings S1 and S2 of equal length, which consist only of the characters ‘2’, ‘1’ and ‘0’.

  1. Converting ‘1’ to ‘2’ or vice versa costs a.
  2. Converting ‘2’ to ‘3’ or vice versa costs b.
  3. Converting ‘3’ to ‘1’ or vice versa costs c.
  4. Deleting the ith character from both the strings costs d.

The task is to find the minimum cost to make both the strings equal in configuration.

Examples:



Input: s1 = “121”, s2 = “223”, a = 2, b = 3, c = 4, d = 10
Output: 6
Change the first character from ‘1’ to ‘2’ which costs 2.
Change the third character from ‘3’ to ‘1’ which costs 4.

Input: s1 = ‘222’, s2 = ‘111’, a = 20, b = 30, c = 40, d = 1
Output: 3
Delete all characters which costs 3 which is the minimum possible.

Approach:

  • Iterate in the string, and if s1[i] = s2[i] then perform no operation.
  • If the s1[i] = ‘1’ and s2[i] = ‘2’ then perform the minimum cost operation from the following:
    1. Delete s1[i] and s2[i] which costs d.
    2. Change ‘1’ to ‘2’ which costs a
    3. Change ‘1’ to ‘3’ and then ‘3’ to ‘2’ which costs b + c.
  • If the s1[i] = ‘2’ and s2[i] = ‘3’ then perform the minimum cost operation from the following:
    1. Delete s1[i] and s2[i] which costs d.
    2. Change ‘2’ to ‘3’ which costs b.
    3. Change ‘2’ to ‘1’ and then ‘1’ to ‘3’ which costs a + c.
  • If the s1[i] = ‘3’ and s2[i] = ‘1’ then perform the minimum cost operation from the following:
    1. Delete s1[i] and s2[i] which costs d.
    2. Change ‘3’ to ‘1’ which costs c.
    3. Change ‘3’ to ‘2’ and then ‘2’ to ‘1’ which costs b + a.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the minimum cost to make the
// configuration of both the strings same
int findCost(string s1, string s2,
             int a, int b, int c, int d, int n)
{
    int cost = 0;
  
    // Iterate and find the cost
    for (int i = 0; i < n; i++) {
        if (s1[i] == s2[i])
            continue;
        else {
  
            // Find the minimum cost
            if ((s1[i] == '1' && s2[i] == '2')
                || (s2[i] == '1' && s1[i] == '2'))
                cost += min(d, min(a, b + c));
            else if ((s1[i] == '2' && s2[i] == '3')
                     || (s2[i] == '2' && s1[i] == '3'))
                cost += min(d, min(b, a + c));
            else if ((s1[i] == '1' && s2[i] == '3')
                     || (s2[i] == '1' && s1[i] == '3'))
                cost += min(d, min(c, a + b));
        }
    }
    return cost;
}
  
// Driver Code
int main()
{
    string s1 = "121";
    string s2 = "223";
    int a = 2, b = 3, c = 4, d = 10;
    int n = s1.size();
    cout << findCost(s1, s2, a, b, c, d, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG
{
      
// Function to return the minimum cost to make the
// configuration of both the strings same
static int findCost(String s1, String s2,
                    int a, int b, int c, 
                    int d, int n)
{
    int cost = 0;
  
    // Iterate and find the cost
    for (int i = 0; i < n; i++) 
    {
        if (s1.charAt(i) == s2.charAt(i))
            continue;
        else 
        {
  
            // Find the minimum cost
            if ((s1.charAt(i) == '1' && s2.charAt(i) == '2') || 
                (s2.charAt(i) == '1' && s1.charAt(i) == '2'))
                cost += Math.min(d, Math.min(a, b + c));
            else if ((s1.charAt(i) == '2' && s2.charAt(i) == '3') || 
                     (s2.charAt(i) == '2' && s1.charAt(i) == '3'))
                cost += Math.min(d, Math.min(b, a + c));
            else if ((s1.charAt(i) == '1' && s2.charAt(i) == '3') ||
                     (s2.charAt(i) == '1' && s1.charAt(i) == '3'))
                cost += Math.min(d, Math.min(c, a + b));
        }
    }
    return cost;
}
  
// Driver Code
public static void main(String[] args)
{
    String s1 = "121";
    String s2 = "223";
    int a = 2, b = 3, c = 4, d = 10;
    int n = s1.length();
    System.out.println(findCost(s1, s2, a, b, c, d, n));
}
}
  
// This code is contributed by Code_Mech.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 implementation of the approach
  
# Function to return the minimum cost to make 
# the configuration of both the strings same
def findCost(s1, s2, a, b, c, d, n):
    cost = 0
  
    # Iterate and find the cost
    for i in range(n):
        if (s1[i] == s2[i]):
            continue
        else:
              
            # Find the minimum cost
            if ((s1[i] == '1' and s2[i] == '2') or 
                (s2[i] == '1' and s1[i] == '2')):
                cost += min(d, min(a, b + c))
            elif ((s1[i] == '2' and s2[i] == '3') or
                  (s2[i] == '2' and s1[i] == '3')):
                cost += min(d, min(b, a + c))
            elif ((s1[i] == '1' and s2[i] == '3') or
                  (s2[i] == '1' and s1[i] == '3')):
                cost += min(d, min(c, a + b))
    return cost
  
# Driver Code
if __name__ == '__main__':
    s1 = "121"
    s2 = "223"
    a = 2
    b = 3
    c = 4
    d = 10
    n = len(s1)
    print(findCost(s1, s2, a, b, c, d, n))
  
# This code is contributed by
# Surendra_Gangwar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG
{
      
// Function to return the minimum cost to make the
// configuration of both the strings same
static int findCost(string s1, string s2,
            int a, int b, int c, int d, int n)
{
    int cost = 0;
  
    // Iterate and find the cost
    for (int i = 0; i < n; i++) 
    {
        if (s1[i] == s2[i])
            continue;
        else 
        {
  
            // Find the minimum cost
            if ((s1[i] == '1' && s2[i] == '2')
                || (s2[i] == '1' && s1[i] == '2'))
                cost +=Math.Min(d,Math.Min(a, b + c));
            else if ((s1[i] == '2' && s2[i] == '3')
                    || (s2[i] == '2' && s1[i] == '3'))
                cost +=Math.Min(d,Math.Min(b, a + c));
            else if ((s1[i] == '1' && s2[i] == '3')
                    || (s2[i] == '1' && s1[i] == '3'))
                cost +=Math.Min(d,Math.Min(c, a + b));
        }
    }
    return cost;
}
  
// Driver Code
public static void Main()
{
    string s1 = "121";
    string s2 = "223";
    int a = 2, b = 3, c = 4, d = 10;
    int n = s1.Length;
    Console.WriteLine(findCost(s1, s2, a, b, c, d, n));
}
}
  
// This Code is Contributed by Code_Mech.

chevron_right


Output:

6


My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.