Count minimum character replacements required such that given string satisfies the given conditions | Function that finds the minimum count of steps required to make the string special ; Stores the frequency of the left & right half of string ; Find frequency of left half ; Find frequency of left half ; Make all characters equal to character c ; Case 1 : For s [ i ] < s [ j ] ; Subtract all the characters on left side that are <= d ; Adding all characters on the right side that same as d ; Find minimum value of count ; Similarly for Case 2 : s [ i ] > s [ j ] ; Return the minimum changes ; Given string S ; Function Call
function minChange ( s , n ) { var L = new Array ( 26 ) . fill ( 0 ) ; var R = new Array ( 26 ) . fill ( 0 ) ; for ( var i = 0 ; i < n / 2 ; i ++ ) { var ch = s [ i ] . charCodeAt ( 0 ) ; L [ ch - " " . charCodeAt ( 0 ) ] ++ ; } for ( var i = n / 2 ; i < n ; i ++ ) { var ch = s [ i ] . charCodeAt ( 0 ) ; R [ ch - " " . charCodeAt ( 0 ) ] ++ ; } var count = n ; for ( var ch = " " . charCodeAt ( 0 ) ; ch <= " " . charCodeAt ( 0 ) ; ch ++ ) { count = Math . min ( count , n - L [ ch - " " . charCodeAt ( 0 ) ] - R [ ch - " " . charCodeAt ( 0 ) ] ) ; } var change = parseInt ( n / 2 ) ; for ( var d = 0 ; d + 1 < 26 ; d ++ ) { change -= L [ d ] ; change += R [ d ] ; count = Math . min ( count , change ) ; } change = n / 2 ; for ( var d = 0 ; d + 1 < 26 ; d ++ ) { change -= R [ d ] ; change += L [ d ] ; count = Math . min ( change , count ) ; } return count ; } var S = " " ; var N = S . length ; document . write ( minChange ( S , N ) ) ;