Minimize ASCII values sum after removing all occurrences of one character | Function to return the minimized sum ; To store the occurrences of each character of the string ; Update the occurrence ; Calculate the sum ; Get the character which is contributing the maximum value to the sum ; Count of occurrence of the character multiplied by its ASCII value ; Return the minimized sum ; Driver code
function getMinimizedSum ( str , len ) { var i , maxVal = - 2147483648 , sum = 0 ; var occurrences = new Array ( 26 ) . fill ( 0 ) ; for ( i = 0 ; i < len ; i ++ ) { occurrences [ str [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ++ ; sum += str [ i ] . charCodeAt ( 0 ) ; } for ( i = 0 ; i < 26 ; i ++ ) { maxVal = Math . max ( maxVal , occurrences [ i ] * ( i + " " . charCodeAt ( 0 ) ) ) ; } return sum - maxVal ; } var str = " " ; var len = str . length ; document . write ( getMinimizedSum ( str , len ) ) ;