Minimize replacements by previous or next alphabet required to make all characters of a string the same | Function to find the minimum count of operations to make all characters of the string same ; Set min to some large value ; Find minimum operations for each character ; Initialize cnt ; Add the value to cnt ; Update minValue ; Return minValue ; Given string str ; Function Call
function minCost ( s , n ) { var minValue = 100000000 ; for ( var i = 0 ; i <= 25 ; i ++ ) { var cnt = 0 ; for ( var j = 0 ; j < n ; j ++ ) { cnt += Math . min ( Math . abs ( i - ( s [ j ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ) ) , 26 - Math . abs ( i - ( s [ j ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ) ) ) ; } minValue = Math . min ( minValue , cnt ) ; } return minValue ; } var str = " " ; var N = str . length ; document . write ( minCost ( str , N ) ) ;