Acronym words | C ++ implementation of the approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count_acronym ( int n , string arr [ ] ) { int freq [ 26 ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) freq [ arr [ i ] [ 0 ] - ' a ' ] ++ ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { string st = arr [ i ] ; int num [ 26 ] = { 0 } ; for ( int j = 0 ; j < st . length ( ) ; j ++ ) num [ st [ j ] - ' a ' ] ++ ; bool flag = true ; for ( int j = 1 ; j < 26 ; j ++ ) { if ( num [ j ] > freq [ j ] ) { flag = false ; break ; } } int x = st [ 0 ] - ' a ' ; if ( freq [ x ] - 1 < num [ x ] ) flag = false ; if ( flag ) cnt ++ ; } return cnt ; } int main ( ) { string arr [ ] = { " abc " , " bcad " , " cabd " , " cba " , " dzzz " } ; int n = 5 ; cout << count_acronym ( n , arr ) ; }