Check if frequency of each character is equal to its position in English Alphabet | C ++ program for the above approach ; Initialise frequency array ; Traverse the string ; Update the frequency ; Check for valid string ; If frequency is non - zero ; If freq is not equals to ( i + 1 ) , then return false ; Return true ; ; Driver Code ; Given string str
#include " bits / stdc + + . h " NEW_LINE using namespace std ; bool checkValidString ( string str ) { int freq [ 26 ] = { 0 } ; for ( int i = 0 ; str [ i ] ; i ++ ) { freq [ str [ i ] - ' a ' ] ++ ; } for ( int i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] != 0 ) { if ( freq [ i ] != i + 1 ) { return false ; } } } return true ; } int main ( ) { string str = " abbcccdddd " ; if ( checkValidString ( str ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }