#K33247. Count Characters in Formable Words

    ID: 25045 Type: Default 1000ms 256MiB

Count Characters in Formable Words

Count Characters in Formable Words

You are given a list of words and a string of characters. Your task is to determine the total length of all the words that can be formed using the characters from the given string. Each character in the character string can be used only once per word. Formally, for each word (w) and character string (s), the word (w) can be formed if for every character (c) in (w), the frequency of (c) in (w) is less than or equal to its frequency in (s).

For example, if the word is "cat" and the string is "atach", since 'c', 'a', and 't' appear sufficiently, the word can be formed and its length is counted towards the total.

inputFormat

The input is given from standard input (stdin) and consists of three parts:

  1. An integer (n) representing the number of words.
  2. A line containing (n) words separated by spaces.
  3. A string representing the available characters.

You can assume that all words and the character string consist of lowercase English letters.

outputFormat

Output the total length of all words that can be formed using the characters from the given character string, printed to standard output (stdout).## sample

4
cat bt hat tree
atach
6

</p>