Least number of manipulations needed to ensure two strings have identical characters | Java program to count least number of manipulations to have two strings set of same characters ; return the count of manipulations required ; count the number of different characters in both strings ; check the difference in characters by comparing count arrays ; driver program
import java . io . * ; public class GFG { static int MAX_CHAR = 26 ; static int leastCount ( String s1 , String s2 , int n ) { int [ ] count1 = new int [ MAX_CHAR ] ; int [ ] count2 = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < n ; i ++ ) { count1 [ s1 . charAt ( i ) - ' a ' ] += 1 ; count2 [ s2 . charAt ( i ) - ' a ' ] += 1 ; } int res = 0 ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) { if ( count1 [ i ] != 0 ) { res += Math . abs ( count1 [ i ] - count2 [ i ] ) ; } } return res ; } static public void main ( String [ ] args ) { String s1 = " abc " ; String s2 = " cdd " ; int len = s1 . length ( ) ; int res = leastCount ( s1 , s2 , len ) ; System . out . println ( res ) ; } }