Given a string str, the task is to minimize the sum of ASCII values of each character of str after removing every occurrence of a particular character.
Input: str = “geeksforgeeks”
‘g’ occurs twice -> 2 * 103 = 206
‘e’ occurs 4 times -> 4 * 101 = 404
‘k’ occurs twice -> 2 * 107 = 214
‘s’ occurs twice -> 2 * 115 = 230
‘f’ occurs once -> 1 * 102 = 102
‘o’ occurs once -> 1 * 111 = 111
‘r’ occurs once -> 1 * 114 = 114
Total sum = 1381
In order to minimize the sum, remove all the occurrences of ‘e’ from the string
And the new sum becomes 1381 – 404 = 977
Input: str = “abcd”
- Take the sum of all ASCII values in the given string.
- Also store the occurrences of each of the character of the string.
- Remove every occurrence of the character which is contributing the maximum value to the sum i.e. whose occurrence * ASCII is maximum.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Map every character of one string to another such that all occurrences are mapped to the same character
- Minimize the length of string by removing occurrence of only one character
- Replace all occurrences of character X with character Y in given string
- Replace every character of string by character whose ASCII value is K times more than it
- Minimum sum of squares of character counts in a given string after removing k characters
- Smallest string obtained by removing all occurrences of 01 and 11 from Binary String
- Map function and Dictionary in Python to sum ASCII values
- Count the number of words having sum of ASCII values less than and greater than k
- Find the sum of the ascii values of characters which are present at prime positions
- Split a given string into substrings of length K with equal sum of ASCII values
- Character replacement after removing duplicates from a string
- Lexicographically smallest string formed by removing at most one character
- Minimize length of Substrings containing at least one common Character
- Check if all occurrences of a character appear together
- Remove all occurrences of a character in a string
- Number of permutations of a string in which all the occurrences of a given character occurs together
- Remove all occurrences of a character in a string | Recursive approach
- Average of ASCII values of characters of a given string
- Check if a string contains only alphabets in Java using ASCII values
- Program to find the product of ASCII values of characters in a string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.