Quick way to check if all the characters of a string are same | C ++ program for above approach ; Function to check is all the characters in string are or not ; Insert characters in the set ; If all characters are same Size of set will always be 1 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void allCharactersSame ( string s ) { set < char > s1 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) s1 . insert ( s [ i ] ) ; if ( s1 . size ( ) == 1 ) cout << " YES " ; else cout << " NO " ; } int main ( ) { string str = " nnnn " ; allCharactersSame ( str ) ; return 0 ; }