Acronym words | Function to return the number of strings that can be an acronym for other strings ; Frequency array to store the frequency of the first character of every string in the array ; To store the count of required strings ; Current word ; Frequency array to store the frequency of each of the character of the current string ; Check if the frequency of every character in the current string is <= its value in freq [ ] ; First character of the current string ; Driver code
function count_acronym ( n , arr ) { let freq = new Array ( 26 ) ; freq . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) freq [ arr [ i ] [ 0 ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ++ ; let cnt = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let st = arr [ i ] ; let num = new Array ( 26 ) ; num . fill ( 0 ) ; for ( let j = 0 ; j < st . length ; j ++ ) num [ st [ j ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ++ ; let flag = true ; for ( let j = 1 ; j < 26 ; j ++ ) { if ( num [ j ] > freq [ j ] ) { flag = false ; break ; } } let x = st [ 0 ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ; if ( freq [ x ] - 1 < num [ x ] ) flag = false ; if ( flag ) cnt ++ ; } return cnt ; } let arr = [ " " , " " , " " , " " , " " ] ; let n = arr . length ; document . write ( count_acronym ( n , arr ) ) ;