Minimize ASCII values sum after removing all occurrences of one character | Java implementation of the approach ; 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
import java . util . Arrays ; import java . lang . Math ; class GfG { static int getMinimizedSum ( String str , int len ) { int i , maxVal = Integer . MIN_VALUE , sum = 0 ; int occurrences [ ] = new int [ 26 ] ; Arrays . fill ( occurrences , 0 ) ; for ( i = 0 ; i < len ; i ++ ) { occurrences [ str . charAt ( i ) - ' a ' ] ++ ; sum += ( int ) str . charAt ( i ) ; } for ( i = 0 ; i < 26 ; i ++ ) maxVal = Math . max ( maxVal , occurrences [ i ] * ( i + ' a ' ) ) ; return ( sum - maxVal ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; int len = str . length ( ) ; System . out . println ( getMinimizedSum ( str , len ) ) ; } }