Home  >  Article  >  Backend Development  >  Make all characters in the string the same by increasing or decreasing the minimum ASCII value

Make all characters in the string the same by increasing or decreasing the minimum ASCII value

王林
王林forward
2023-08-26 12:53:121346browse

Make all characters in the string the same by increasing or decreasing the minimum ASCII value

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 Questions

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 operate by incrementing or decrementing the ASCII value of the character. Each increment or decrement counts as an operation. The goal is to find the minimum number of operations required to make all characters in a string the same.

method

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 characters with the highest frequency. The number of operations required to make all characters the same as this character will be the sum of the differences between the ASCII values ​​of the most frequent characters and the ASCII values ​​of all other characters.

C Solution

The Chinese translation of

Example

is:

Example

The following is the C code to solve 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 saw how to solve unique problems involving ASCII value and string manipulation in C.

The above is the detailed content of Make all characters in the string the same by increasing or decreasing the minimum ASCII value. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete