Minimum Cost To Make Two Strings Identical
Last Updated :
20 Dec, 2022
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.
Examples :
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.
C++
#include<bits/stdc++.h>
using namespace std;
int lcs( char *X, char *Y, int m, int n)
{
int L[m+1][n+1];
for ( int i=0; i<=m; i++)
{
for ( int j=0; j<=n; j++)
{
if (i == 0 || j == 0)
L[i][j] = 0;
else if (X[i-1] == Y[j-1])
L[i][j] = L[i-1][j-1] + 1;
else
L[i][j] = max(L[i-1][j], L[i][j-1]);
}
}
return L[m][n];
}
int findMinCost( char X[], char Y[], int costX, int costY)
{
int m = strlen (X), n = strlen (Y);
int len_LCS = lcs(X, Y, m, n);
return costX * (m - len_LCS) +
costY * (n - len_LCS);
}
int main()
{
char X[] = "ef" ;
char Y[] = "gh" ;
cout << "Minimum Cost to make two strings "
<< " identical is = " << findMinCost(X, Y, 10, 20);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int lcs(String X, String Y, int m, int n)
{
int L[][]= new int [m + 1 ][n + 1 ];
for ( int i = 0 ; i <= m; i++)
{
for ( int j = 0 ; j <= n; j++)
{
if (i == 0 || j == 0 )
L[i][j] = 0 ;
else if (X.charAt(i - 1 ) == Y.charAt(j - 1 ))
L[i][j] = L[i - 1 ][j - 1 ] + 1 ;
else
L[i][j] = Math.max(L[i - 1 ][j], L[i][j - 1 ]);
}
}
return L[m][n];
}
static int findMinCost(String X, String Y, int costX, int costY)
{
int m = X.length();
int n = Y.length();
int len_LCS;
len_LCS = lcs(X, Y, m, n);
return costX * (m - len_LCS) +
costY * (n - len_LCS);
}
public static void main (String[] args)
{
String X = "ef" ;
String Y = "gh" ;
System.out.println( "Minimum Cost to make two strings "
+ " identical is = "
+ findMinCost(X, Y, 10 , 20 ));
}
}
|
Python3
def lcs(X, Y, m, n):
L = [[ 0 for i in range (n + 1 )]
for i in range (m + 1 )]
for i in range (m + 1 ):
for j in range (n + 1 ):
if i = = 0 or j = = 0 :
L[i][j] = 0
else if X[i - 1 ] = = Y[j - 1 ]:
L[i][j] = L[i - 1 ][j - 1 ] + 1
else :
L[i][j] = max (L[i - 1 ][j],
L[i][j - 1 ])
return L[m][n]
def findMinCost(X, Y, costX, costY):
m = len (X)
n = len (Y)
len_LCS = lcs(X, Y, m, n)
return (costX * (m - len_LCS) +
costY * (n - len_LCS))
X = "ef"
Y = "gh"
print ( 'Minimum Cost to make two strings ' , end = '')
print ( 'identical is = ' , findMinCost(X, Y, 10 , 20 ))
|
C#
using System;
class GFG {
static int lcs(String X, String Y, int m, int n)
{
int [,]L = new int [m + 1, n + 1];
for ( int i = 0; i <= m; i++)
{
for ( int j = 0; j <= n; j++)
{
if (i == 0 || j == 0)
L[i,j] = 0;
else if (X[i - 1] == Y[j - 1])
L[i,j] = L[i - 1,j - 1] + 1;
else
L[i,j] = Math.Max(L[i - 1,j], L[i,j - 1]);
}
}
return L[m,n];
}
static int findMinCost(String X, String Y,
int costX, int costY)
{
int m = X.Length;
int n = Y.Length;
int len_LCS;
len_LCS = lcs(X, Y, m, n);
return costX * (m - len_LCS) +
costY * (n - len_LCS);
}
public static void Main ()
{
String X = "ef" ;
String Y = "gh" ;
Console.Write( "Minimum Cost to make two strings " +
" identical is = " +
findMinCost(X, Y, 10, 20));
}
}
|
PHP
<?php
function lcs( $X , $Y , $m , $n )
{
$L = array_fill (0,( $m +1), array_fill (0,( $n +1),NULL));
for ( $i =0; $i <= $m ; $i ++)
{
for ( $j =0; $j <= $n ; $j ++)
{
if ( $i == 0 || $j == 0)
$L [ $i ][ $j ] = 0;
else if ( $X [ $i -1] == $Y [ $j -1])
$L [ $i ][ $j ] = $L [ $i -1][ $j -1] + 1;
else
$L [ $i ][ $j ] = max( $L [ $i -1][ $j ], $L [ $i ][ $j -1]);
}
}
return $L [ $m ][ $n ];
}
function findMinCost(& $X , & $Y , $costX , $costY )
{
$m = strlen ( $X );
$n = strlen ( $Y );
$len_LCS = lcs( $X , $Y , $m , $n );
return $costX * ( $m - $len_LCS ) +
$costY * ( $n - $len_LCS );
}
$X = "ef" ;
$Y = "gh" ;
echo "Minimum Cost to make two strings " .
" identical is = " . findMinCost( $X , $Y , 10, 20);
return 0;
?>
|
Javascript
<script>
function lcs(X, Y, m, n)
{
let L = new Array(m+1);
for (let i = 0; i < m + 1; i++)
{
L[i] = new Array(n + 1);
}
for (let i = 0; i < m + 1; i++)
{
for (let j = 0; j < n + 1; j++)
{
L[i][j] = 0;
}
}
for (let i = 0; i <= m; i++)
{
for (let j = 0; j <= n; j++)
{
if (i == 0 || j == 0)
L[i][j] = 0;
else if (X[i-1] == Y[j-1])
L[i][j] = L[i - 1][j - 1] + 1;
else
L[i][j] = Math.max(L[i - 1][j], L[i][j - 1]);
}
}
return L[m][n];
}
function findMinCost(X,Y,costX,costY)
{
let m = X.length;
let n = Y.length;
let len_LCS;
len_LCS = lcs(X, Y, m, n);
return costX * (m - len_LCS) +
costY * (n - len_LCS);
}
let X = "ef" ;
let Y = "gh" ;
document.write( "Minimum Cost to make two strings "
+ " identical is = "
+ findMinCost(X, Y, 10, 20));
</script>
|
Output
Minimum Cost to make two strings identical is = 60
Time Complexity: O(m*n)
Auxiliary Space: O(m*n)
This article is reviewed by team geeksforgeeks.
Share your thoughts in the comments
Please Login to comment...