Least number of manipulations needed to ensure two strings have identical characters | Javascript 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 Code
let MAX_CHAR = 26 ; function leastCount ( s1 , s2 , n ) { let count1 = new Array ( MAX_CHAR ) ; let count2 = new Array ( MAX_CHAR ) ; for ( let i = 0 ; i < MAX_CHAR ; i ++ ) { count1 [ i ] = 0 ; count2 [ i ] = 0 ; } for ( let i = 0 ; i < n ; i ++ ) { count1 [ s1 [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] += 1 ; count2 [ s2 [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] += 1 ; } let res = 0 ; for ( let i = 0 ; i < MAX_CHAR ; i ++ ) { if ( count1 [ i ] != 0 ) { res += Math . abs ( count1 [ i ] - count2 [ i ] ) ; } } return res ; } let s1 = " " ; let s2 = " " ; let len = s1 . length ; let res = leastCount ( s1 , s2 , len ) ; document . write ( res ) ;