Minimum cost to convert one given string to another using swap , insert or delete operations | C # program for the above approach ; Function to find the minimum cost to convert string a to b ; Stores the frequency of string a and b respectively ; Store the frequencies of characters in a ; Store the frequencies of characters in b ; Minimum cost to convert A to B ; Find the minimum cost ; Print the minimum cost ; Driver code ; Function Call
using System ; using System . Collections . Generic ; class GFG { public static void minimumCost ( string a , string b ) { int [ ] fre1 = new int [ 256 ] ; int [ ] fre2 = new int [ 256 ] ; foreach ( char c in a . ToCharArray ( ) ) fre1 [ ( int ) ( c ) ] ++ ; foreach ( char c in b . ToCharArray ( ) ) fre2 [ ( int ) ( c ) ] ++ ; int mincost = 0 ; for ( int i = 0 ; i < 256 ; i ++ ) { mincost += Math . Abs ( fre1 [ i ] - fre2 [ i ] ) ; } Console . Write ( mincost ) ; } public static void Main ( ) { string A = "1AB + - " , B = " cc " ; minimumCost ( A , B ) ; } }