- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if all characters of a string can be made equal by increments or decrements
In this problem, we need to check if we can make all characters of strings equal by increment and decrement operations. We can get the weight of each character based on their ASCII values and check whether the total weight can be used to make all characters equal.
Problem statement – We have given string str of length N containing lowercase alphabetical characters. We need to check whether we can make all characters of the string equal by choosing any of two characters, increasing one, and decreasing another by 1. If possible, print ‘yes’, else ‘no’.
Sample examples
Input– str = ‘aedb
Output – str = ‘aedb
Explanation – ‘a’ can be increased by 2, and ‘e’ can be decreased by 2. Also, ‘b’ can be increased by 1, and ‘d’ can be increased by 1. So, the resultant string can be ‘cccc’.
Input– str = ‘abd’
Output – ‘No’
Explanation – We can’t make all characters of the string equal by increment and decrement operations
Input– ‘g’
Output – ‘Yes’
Explanation – The string contains a single character only, so all string characters are already equal
Approach 1
In this approach, we will count the total weight of characters of the string. The weight of characters is defined as ‘a’ = 1, ‘b’ = 2, ‘c’ = 3, …., ‘z’ = 26. So, if we divide the total weight by the length of the string, we can say that we can make all characters of the string equal by increasing one character and decreasing another character.
Algorithm
Define the ‘len’ variable and store the size of the string using the size() method.
Define the ‘totalWeight’ variable to store the total weight of all characters of the given string
Use each character's ASCII code to get a particular character's weight and add it to the ‘totalWeight’ variable.
If the value of ‘totalWeight’ is divisible by ‘len’, return true. Else, return false.
Example
#include <iostream> using namespace std; // function to check if all characters of a string can be made equal by incrementing or decrementing by 1 bool canMakeEqual(string str){ int len = str.size(); // store sum of ASCII values of characters int totalWeight = 0; // Iterate over the string for (int i = 0; i < len; i++){ // get the ASCII value of each character totalWeight += str[i] - 'a' + 1; } return (totalWeight % len == 0); } int main(){ string str = "aedb"; if (canMakeEqual(str)) cout << "Yes"; else cout << "No"; return 0; }
Output
Yes
Time complexity – O(N) as we traverse the string.
Space complexity – O(1), as we use constant space.
Conclusion
We learned to check whether all characters of string can be made equal by increment and decrement character’s ASCII value. We solve the problem based on the ‘total Weight’. Users can also try to find the resultant string. To find the resultant string, find the ASCII value respected to (totalWeight / len), and add the ‘len’ number of characters in the given string.