Check if frequency of each character is equal to its position in English Alphabet | Javascript 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 ; ; Given String str
function checkValidString ( str ) { var freq = new Array ( 26 ) . fill ( 0 ) ; for ( var i = 0 ; i < str . length ; i ++ ) { freq [ str [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ++ ; } for ( var i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] !== 0 ) { if ( freq [ i ] !== i + 1 ) { return false ; } } } return true ; } var str = " " ; if ( checkValidString ( str ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }