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
function countCharacters ( strings , chars ) { var res = 0 ; var freq = new Map ( ) ; for ( var i = 0 ; i < chars . length ; i ++ ) { if ( freq . has ( chars [ i ] ) ) freq . set ( chars [ i ] , freq . get ( chars [ i ] ) + 1 ) else freq . set ( chars [ i ] , 1 ) } strings . forEach ( st => { var flag = true ; st . split ( ' ' ) . forEach ( c => { if ( ! freq . has ( c ) ) { flag = false ; } } ) ; if ( flag ) res += st . length ; } ) ; return res ; } var strings = [ " " , " " , " " ] ; var chars = " " ; document . write ( countCharacters ( strings , chars ) ) ;