Minimum cost to convert one given string to another using swap , insert or delete operations | Java 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
import java . util . * ; class GFG { public static void minimumCost ( String a , String b ) { int fre1 [ ] = new int [ 256 ] ; int fre2 [ ] = new int [ 256 ] ; for ( char c : a . toCharArray ( ) ) fre1 [ ( int ) ( c ) ] ++ ; for ( char c : b . toCharArray ( ) ) fre2 [ ( int ) ( c ) ] ++ ; int mincost = 0 ; for ( int i = 0 ; i < 256 ; i ++ ) { mincost += Math . abs ( fre1 [ i ] - fre2 [ i ] ) ; } System . out . println ( mincost ) ; } public static void main ( String [ ] args ) { String A = "1AB + - " , B = " cc " ; minimumCost ( A , B ) ; } }