Total length of string from given Array of strings composed using given characters | Function to count the total length ; Unordered_map for keeping frequency of characters ; Calculate the frequency ; Iterate in the N strings ; Iterates in the string ; Checks if given character of string string appears in it or not ; Adds the length of string if all characters are present ; Return the final result ; Driver code
def countCharacters ( arr , chars ) : NEW_LINE INDENT res = 0 NEW_LINE freq = dict ( ) NEW_LINE for i in range ( len ( chars ) ) : NEW_LINE INDENT freq [ chars [ i ] ] = freq . get ( chars [ i ] , 0 ) + 1 NEW_LINE DEDENT for st in arr : NEW_LINE INDENT flag = True NEW_LINE for c in st : NEW_LINE INDENT if ( c not in freq ) : NEW_LINE INDENT flag = False NEW_LINE break NEW_LINE DEDENT DEDENT if ( flag ) : NEW_LINE INDENT res += len ( st ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ " hi " , " data " , " geeksforgeeks " ] NEW_LINE chars = " tiadhae " NEW_LINE print ( countCharacters ( arr , chars ) ) NEW_LINE DEDENT