Minimum cost to make two strings same | C ++ implementation of the approach ; Function to return the minimum cost to make the configuration of both the strings same ; Iterate and find the cost ; Find the minimum cost ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findCost ( string s1 , string s2 , int a , int b , int c , int d , int n ) { int cost = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s1 [ i ] == s2 [ i ] ) continue ; else { 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 ; } 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 ; }