Check if all characters of a string can be made equal by increments or decrements
Given a string S consisting of N lowercase alphabets, the task is to check if it is possible to make all characters of the string S equal by incrementing and decrementing any two characters from the given string by 1. If it is possible to make all the characters the same, then print “Yes”. Otherwise, print “No”.
Incrementing or decrementing any character means that change the characters to its next or previous character in the English alphabet respectively. If the characters are ‘a’ and ‘z’ then they cannot be changed further.
Examples:
Input: S = “beb”
Output: Yes
Explanation:
The characters of the given string S can be made equal by performing the below operations:
- For string “beb” increment ‘b’ by 1 and decrement ‘e’ by 1, then the string becomes “cdb”.
- For string “cdb” decrement ‘d’ by 1 and increment ‘b’ by 1, then the string becomes “ccc”.
Input: S = “geeks”
Output: No
Approach: The given problem can be solved based on the below observations:
- While incrementing and decrementing any character by 1 at the same time, the sum of ASCII values of the string remains the same before and after the operation.
- Let suppose the ASCII value of any character is X. If all the characters are equal then the sum ASCII value of the string is N*X. Hence, it can be said that the sum is divisible by N. Therefore, the initial sum of the ASCII value needs to be divisible by N in order to make all the characters equal.
Follow the below steps to solve this problem:
- Initialize a variable sum, that stores the sum of the ASCII value of the given string.
- Traverse the given string S and for each character S[i] add the value of (S[i] – ‘a’ + 1) to the sum.
- After completing the above steps, if the value of sum is divisible by N then all the characters of the given string can be made equal. Therefore, print “Yes”. Otherwise, print “No”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void canMakeEqual(string S)
{
int N = S.size();
int weightOfString = 0;
for ( int i = 0; i < N; i++) {
weightOfString += S[i] - 'a' + 1;
}
if (weightOfString % N == 0)
cout << "Yes" ;
else
cout << "No" ;
}
int main()
{
string S = "beb" ;
canMakeEqual(S);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void canMakeEqual(String S){
int N = S.length();
int weightOfString = 0 ;
for ( int i = 0 ; i < N; i++) {
weightOfString += S.charAt(i) - 'a' + 1 ;
}
if (weightOfString % N == 0 )
System.out.println( "Yes" );
else
System.out.println( "No" );
}
public static void main (String[] args) {
String S = "beb" ;
canMakeEqual(S);
}
}
|
Python3
def canMakeEqual(S):
N = len (S)
weightOfString = 0
for i in range (N):
weightOfString + = ord (S[i]) - ord ( 'a' ) + 1
if (weightOfString % N = = 0 ):
print ( "Yes" )
else :
print ( "No" )
S = "beb"
canMakeEqual(S)
|
C#
using System;
class GFG{
static void canMakeEqual(String S)
{
int N = S.Length;
int weightOfString = 0;
for ( int i = 0; i < N; i++)
{
weightOfString += S[i] - 'a' + 1;
}
if (weightOfString % N == 0)
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
public static void Main(String[] args)
{
String S = "beb" ;
canMakeEqual(S);
}
}
|
Javascript
<script>
function canMakeEqual(S){
var N = S.length;
var weightOfString = 0;
for ( var i = 0; i < N; i++) {
weightOfString += S.charAt(i).charCodeAt(0) -
'a' .charCodeAt(0) + 1;
}
if (weightOfString % N == 0)
document.write( "Yes" );
else
document.write( "No" );
}
var S = "beb" ;
canMakeEqual(S);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
01 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...