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
def count_acronym ( n , arr ) : NEW_LINE INDENT freq = [ 0 ] * 26 NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq [ ord ( arr [ i ] [ 0 ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT cnt = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT st = arr [ i ] NEW_LINE num = [ 0 ] * 26 NEW_LINE for j in range ( len ( st ) ) : NEW_LINE INDENT num [ ord ( st [ j ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT flag = True NEW_LINE for j in range ( 1 , 26 ) : NEW_LINE INDENT if num [ j ] > freq [ j ] : NEW_LINE INDENT flag = False NEW_LINE break NEW_LINE DEDENT DEDENT x = ord ( st [ 0 ] ) - ord ( ' a ' ) NEW_LINE if freq [ x ] - 1 < num [ x ] : NEW_LINE INDENT flag = False NEW_LINE DEDENT if flag : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT return cnt NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ " abc " , " bcad " , " cabd " , " cba " , " dzzz " ] NEW_LINE n = 5 NEW_LINE print ( count_acronym ( n , arr ) ) NEW_LINE DEDENT