
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Minimize ASCII values sum after removing all occurrences of one character in C++
Suppose we have a string. We have to minimize the sum of ASCII values, of each character to the string, after removing every occurrence of a particular character. Suppose a string is given like “hello” the sum of ASCII characters is (104 + 101 + 108 + 108 + 111) = 532. Now check occurrences of each characters.
- h has occurred one time, so cost is 1 * 104 = 104
- e has occurred one time, so cost is 1 * 101 = 101
- l has occurred one time, so cost is 2 * 108 = 216
- o has occurred one time, so cost is 1 * 111 = 111
Here l has occurred maximum amount of time, so if we remove all occurrences of l, then the value will be minimized. So actually we are removing the max value from the above list. Here the final result will be 532 – 216 = 316
The logic is simple at first we have to take the ASCII sum of the string. Then count the frequency of each character present in the string, then remove that character which is contributing max value as occurrence * ASCII value. The subtracted value is the result.
Example
#include <iostream> using namespace std; int minASCIISum(string str, int len) { int max_val = INT_MIN, sum = 0; int frequency[26] = { 0 }; for (int i = 0; i < len; i++) { frequency[str[i] - 'a']++; sum += (int)str[i]; } for (int i = 0; i < 26; i++) max_val = max(max_val, frequency[i] * (i + 'a')); return (sum - max_val); } int main() { string str = "hello"; int n = str.length(); cout << "Minimized Sum: " << minASCIISum(str, n); }
Output
Minimized Sum: 316
- Related Articles
- Maximize the maximum subarray sum after removing at most one element in C++
- Minimize length of a string by removing occurrences of another string from it as a substring
- C Program to print all ASCII values.
- Removing minimize/maximize buttons in Tkinter
- Check if all occurrences of a character appear together in Python
- Java Program to replace all occurrences of a given character with new character
- Maximum sum subarray removing at most one element in C++
- Convert an int to ASCII character in C/C++
- Java Program to replace all occurrences of a given character in a string
- Minimize swaps of same-indexed characters to make sum of ASCII value of characters of both the strings odd
- Count occurrences of a character in a repeated string in C++
- C++ Program to Find ASCII Value of a Character
- C# program to count the occurrences of each character
- Map function and Dictionary in Python to sum ASCII values
- Minimize the sum of roots of a given polynomial in C++
