Minimize characters to be changed to make the left and right rotation of a string same | Function to find the minimum characters to be removed from the string ; Initialize answer by N ; If length is even ; Frequency array for odd and even indices ; Store the frequency of the characters at even and odd indices ; Stores the most occuring frequency for even and odd indices ; Update the answer ; If length is odd ; Stores the frequency of the characters of the string ; Stores the most occuring character in the string ; Update the answer ; Driver code
function getMinimumRemoval ( str ) { var n = str . length ; var ans = n ; if ( n % 2 === 0 ) { var freqEven = new Array ( 128 ) . fill ( 0 ) ; var freqOdd = new Array ( 128 ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( i % 2 === 0 ) { freqEven [ str [ i ] . charCodeAt ( 0 ) ] ++ ; } else { freqOdd [ str [ i ] . charCodeAt ( 0 ) ] ++ ; } } var evenMax = 0 , oddMax = 0 ; for ( var chr = " " . charCodeAt ( 0 ) ; chr <= " " . charCodeAt ( 0 ) ; chr ++ ) { evenMax = Math . max ( evenMax , freqEven [ chr ] ) ; oddMax = Math . max ( oddMax , freqOdd [ chr ] ) ; } ans = ans - evenMax - oddMax ; } else { var freq = new Array ( 128 ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) { freq [ str [ i ] . charCodeAt ( 0 ) ] ++ ; } var strMax = 0 ; for ( var chr = " " . charCodeAt ( 0 ) ; chr <= " " . charCodeAt ( 0 ) ; chr ++ ) { strMax = Math . max ( strMax , freq [ chr ] ) ; } ans = ans - strMax ; } return ans ; } var str = " " ; document . write ( getMinimumRemoval ( str ) ) ;