Open In App

Check if all characters of a string can be made equal by increments or decrements

Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if it is
// possible to make all characters
// of string S same or not
void canMakeEqual(string S)
{
    // Length of string
    int N = S.size();
 
    // Stores the sum of ASCII value
    int weightOfString = 0;
 
    // Traverse the string S
    for (int i = 0; i < N; i++) {
 
        // Update the weightOfString
        weightOfString += S[i] - 'a' + 1;
    }
 
    // If the sum is divisible by N
    // then print "Yes"
    if (weightOfString % N == 0)
        cout << "Yes";
 
    // Otherwise print "No"
    else
        cout << "No";
}
 
// Driver Code
int main()
{
    string S = "beb";
    canMakeEqual(S);
 
    return 0;
}


Java




// Java program for the above approach
import java.io.*;
 
class GFG {
  
// Function to check if it is
// possible to make all characters
// of string S same or not
static void canMakeEqual(String S){
   // Length of string
    int N = S.length();
 
    // Stores the sum of ASCII value
    int weightOfString = 0;
 
    // Traverse the string S
    for (int i = 0; i < N; i++) {
 
        // Update the weightOfString
        weightOfString += S.charAt(i) - 'a' + 1;
    }
 
    // If the sum is divisible by N
    // then print "Yes"
    if (weightOfString % N == 0)
        System.out.println("Yes");
 
    // Otherwise print "No"
    else
        System.out.println("No");
}
   
  // Driver Code
    public static void main (String[] args) {
            String S = "beb";
               canMakeEqual(S);
    }
}
 
// This code is contributed by aadityaburujwale


Python3




# Python3 program for the above approach
 
# Function to check if it is
# possible to make all characters
# of string S same or not
def canMakeEqual(S):
     
    # Length of string
    N = len(S)
  
    # Stores the sum of ASCII value
    weightOfString = 0
  
    # Traverse the string S
    for i in range(N):
  
        # Update the weightOfString
        weightOfString += ord(S[i]) - ord('a') + 1
     
    # If the sum is divisible by N
    # then print "Yes"
    if (weightOfString % N == 0):
        print("Yes")
  
    # Otherwise print "No"
    else:
        print("No")
 
# Driver Code
S = "beb"
canMakeEqual(S)
 
# This code is contributed by susmitakundugoaldanga


C#




// C# program for the above approach
using System;
 
class GFG{
  
// Function to check if it is
// possible to make all characters
// of string S same or not
static void canMakeEqual(String S)
{
     
    // Length of string
    int N = S.Length;
 
    // Stores the sum of ASCII value
    int weightOfString = 0;
 
    // Traverse the string S
    for(int i = 0; i < N; i++)
    {
         
        // Update the weightOfString
        weightOfString += S[i] - 'a' + 1;
    }
 
    // If the sum is divisible by N
    // then print "Yes"
    if (weightOfString % N == 0)
        Console.WriteLine("Yes");
 
    // Otherwise print "No"
    else
        Console.WriteLine("No");
}
   
// Driver Code
public static void Main(String[] args)
{
    String S = "beb";
    canMakeEqual(S);
}
}
 
// This code is contributed by shikhasingrajput


Javascript




<script>
 
// Javascript program for the above approach
 
// Function to check if it is
// possible to make all characters
// of string S same or not
function canMakeEqual(S){
   // Length of string
    var N = S.length;
 
    // Stores the sum of ASCII value
    var weightOfString = 0;
 
    // Traverse the string S
    for (var i = 0; i < N; i++) {
 
        // Update the weightOfString
        weightOfString += S.charAt(i).charCodeAt(0) -
        'a'.charCodeAt(0) + 1;
    }
 
    // If the sum is divisible by N
    // then print "Yes"
    if (weightOfString % N == 0)
        document.write("Yes");
 
    // Otherwise print "No"
    else
        document.write("No");
}
   
// Driver Code
var S = "beb";
canMakeEqual(S);
 
// This code contributed by Princi Singh
 
</script>


Output: 

Yes

 

Time Complexity: O(N)
Auxiliary Space: O(1)



Last Updated : 01 Jul, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads