Check if frequency of each character is equal to its position in English Alphabet | Java 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
class GFG { static boolean checkValidString ( String str ) { int freq [ ] = new int [ 26 ] ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { freq [ str . charAt ( i ) - ' a ' ] ++ ; } for ( int i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] != 0 ) { if ( freq [ i ] != i + 1 ) { return false ; } } } return true ; } public static void main ( String [ ] args ) { String str = " abbcccdddd " ; if ( checkValidString ( str ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }