Minimize count of given operations required to make two given strings permutations of each other | Java program to implement the above approach ; Function to minimize the count of operations to make str1 and str2 permutations of each other ; Store the frequency of each character of str1 ; Store the frequency of each character of str2 ; Traverse the freq1 [ ] and freq2 [ ] ; If frequency of character in str1 is greater than str2 ; Otherwise ; Store sum of freq1 [ ] ; Store sum of freq2 [ ] ; Driver Code
import java . util . * ; import java . io . * ; import java . lang . Math ; class GFG { static int ctMinEdits ( String str1 , String str2 ) { int N1 = str1 . length ( ) ; int N2 = str2 . length ( ) ; int freq1 [ ] = new int [ 256 ] ; Arrays . fill ( freq1 , 0 ) ; for ( int i = 0 ; i < N1 ; i ++ ) { freq1 [ str1 . charAt ( i ) ] ++ ; } int freq2 [ ] = new int [ 256 ] ; Arrays . fill ( freq2 , 0 ) ; for ( int i = 0 ; i < N2 ; i ++ ) { freq2 [ str2 . charAt ( i ) ] ++ ; } for ( int i = 0 ; i < 256 ; i ++ ) { if ( freq1 [ i ] > freq2 [ i ] ) { freq1 [ i ] = freq1 [ i ] - freq2 [ i ] ; freq2 [ i ] = 0 ; } else { freq2 [ i ] = freq2 [ i ] - freq1 [ i ] ; freq1 [ i ] = 0 ; } } int sum1 = 0 ; int sum2 = 0 ; for ( int i = 0 ; i < 256 ; i ++ ) { sum1 += freq1 [ i ] ; sum2 += freq2 [ i ] ; } return Math . max ( sum1 , sum2 ) ; } public static void main ( final String [ ] args ) { String str1 = " geeksforgeeks " ; String str2 = " geeksforcoder " ; System . out . println ( ctMinEdits ( str1 , str2 ) ) ; } }