Minimize ASCII values sum after removing all occurrences of one character | C # 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
using System ; class GfG { static int getMinimizedSum ( string str , int len ) { int i , maxVal = Int32 . MinValue , sum = 0 ; int [ ] occurrences = new int [ 26 ] ; for ( i = 0 ; i < len ; i ++ ) { occurrences [ str [ i ] - ' a ' ] ++ ; sum += ( int ) str [ i ] ; } for ( i = 0 ; i < 26 ; i ++ ) maxVal = Math . Max ( maxVal , occurrences [ i ] * ( i + ' a ' ) ) ; return ( sum - maxVal ) ; } public static void Main ( ) { string str = " geeksforgeeks " ; int len = str . Length ; Console . WriteLine ( getMinimizedSum ( str , len ) ) ; } }