Make all characters of a string same by minimum number of increments or decrements of ASCII values of characters


The ASCII (American Standard Code for Information Interchange) system is often used in programming to manipulate characters. In this article, we will be examining an interesting problem where we need to make all characters of a string same by the minimum number of increments or decrements of ASCII values of characters. We will provide a detailed explanation of the problem, propose an efficient solution in C++, and analyze its complexity.

Understanding the Problem

Given a string consisting of lowercase English letters, our task is to make all characters in the string the same by changing their ASCII values. The catch is that we need to do this using the smallest number of changes.

We can either increment or decrement the ASCII value of a character, with each increment or decrement counting as one operation. The goal is to find the minimum number of operations needed to make all characters in the string the same.

Approach

To solve this problem, we need to find the character that appears most frequently in the string. The reason is that it would require fewer operations to change all other characters to this most common character.

First, we will count the frequency of each character in the string. Then we will find the character with the maximum frequency. The number of operations required to make all characters the same as this character will be the sum of differences between the ASCII value of the most frequent character and the ASCII values of all other characters.

C++ Solution

Example

Here is the C++ code that solves the problem −

#include<bits/stdc++.h>
using namespace std;

int minOperations(string str) {
   int freq[26] = {0};
   
   for (char c : str) {
      freq[c - 'a']++;
   }
   
   int max_freq = *max_element(freq, freq+26);
   int total_chars = str.length();
   return total_chars - max_freq;
}

int main() {
   string str;
   cout << "Enter the string: ";
   cin >> str;
   cout << "Minimum operations: " << minOperations(str) << endl;
   return 0;
}

Output

Enter the string: Minimum operations: 0

Code Explanation

Consider the string "abcdd". The character 'd' appears twice, more than any other character. Therefore, we should change all other characters to 'd'. The ASCII value of 'd' is 100. The ASCII values of 'a', 'b', and 'c' are 97, 98, and 99, respectively. So, the minimum number of operations will be (100-97) + (100-98) + (100-99) = 3 + 2 + 1 = 6. However, since we need to minimize the number of operations, we will instead decrement the ASCII values of 'a', 'b', and 'c'. In this case, the minimum number of operations will be (97-97) + (98-97) + (99-97) = 0 + 1 + 2 = 3.

Conclusion

In this article, we've seen how to solve a unique problem involving ASCII values and string manipulation in C++.

Updated on: 18-May-2023

126 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements