Check if all characters of a string can be made equal by increments or decrements | C ++ program for the above approach ; Function to check if it is possible to make all characters of string S same or not ; Length of string ; Stores the sum of ASCII value ; Traverse the string S ; Update the weightOfString ; If the sum is divisible by N then print " Yes " ; Otherwise print " No " ; Driver Code
#include <bits/stdc++.h> NEW_LINE 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 ; }