Minimize ASCII values sum after removing all occurrences of one character | Python3 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 sys NEW_LINE def getMinimizedSum ( string , length ) : NEW_LINE INDENT maxVal = - ( sys . maxsize - 1 ) NEW_LINE sum = 0 ; NEW_LINE occurrences = [ 0 ] * 26 ; NEW_LINE for i in range ( length ) : NEW_LINE INDENT occurrences [ ord ( string [ i ] ) - ord ( ' a ' ) ] += 1 ; NEW_LINE sum += ord ( string [ i ] ) ; NEW_LINE DEDENT for i in range ( 26 ) : NEW_LINE INDENT count = occurrences [ i ] * ( i + ord ( ' a ' ) ) NEW_LINE maxVal = max ( maxVal , count ) ; NEW_LINE DEDENT return ( sum - maxVal ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " geeksforgeeks " ; NEW_LINE length = len ( string ) ; NEW_LINE print ( getMinimizedSum ( string , length ) ) ; NEW_LINE DEDENT