Flash Slothmore is a hardworking employee at the Department of Mammal Vehicles in the paradise known as Zootopia. While he is normally a fast and efficient worker who strives to provide prompt and courteous customer service, Flash has a tendency to let his mind wander...

For instance, while stamping new license plates for the DMV, he notices that each plate contains a mixture of letters, digits, and spaces. Focusing on just the letters, he likes to think of what possible words could be formed from the letters in the license plate.

For example, given the license plate: 1s3 PSt, this contains the following letters (ignoring case): p, s, s, t. Possible words that can be formed from those letters are: steps, priests, and passports. Although each of these words contain letters not in the license plate, they do contain all the letters that appear in the license plate (ie. they are a superset of the letters in the license plate).

We call these words, completing words:

A completing word is a word that contains all the letters in a license plate. You should ignore numbers, spaces, and case sensitivity. If a letter appears more than once in the license plate, then it must appear in the completing word the same number of times or more.

So in the example above, while steps is a possible completing word for 1s3 PSt, step is not because it only has one s.

Since Flash is a busy DMV worker, he doesn't have time to consider all possible words that could form a completing word for a given license plate. Instead, he just needs to search a list of possible completing words and select the first one that could be a completing word (not all words in the list are valid completing words, but each list contains at least once valid completing word).

Because you wish to encourage Flash to get back to work, you decided to write him a program that finds the first completing word for a given license plate and set of possible candidate completing words.

Input

You will be given a series of tests cases (one per line) in the following format:

license plate1,word1,word2,...
license plate2,word1,word2,...

Each input test cases consists of a license plate, followed by a series of possible completing words (with each word separated by a comma):

1s3 PSt,step,steps,stripe,stepple
1s3 456,pest,stew,show,looks

Output

For each input test case, you are to emit the test case number along with the first completing word (as described above) in the following format:

{Test Case #}. {First Completing Word}

Note: If there is more than one completing word, pick the first one that appears in the given list of words.

Here is the output for the example input above:

1. steps
2. pest

LeetCode

This programming challenge is inspired by 748. Shortest Completing Word on LeetCode.

Algorithmic Complexity

For each input test case, your solution should have the following targets:

Time Complexity O(WN), where W corresponds to the number of words and N corresponds to the number of characters in the largest word in each input test case.
Space Complexity O(1)

Your solution may be below the targets, but it should not exceed them.

Submission

To submit your work, follow the same procedure you used for Reading 00:

$ cd path/to/cse-30872-fa22-assignments     # Go to assignments repository
$ git checkout master                       # Make sure we are on master
$ git pull --rebase                         # Pull any changes from GitHub

$ git checkout -b challenge01               # Create and checkout challenge01 branch

$ $EDITOR challenge01/program.cpp           # Edit your code

$ git add challenge01/program.cpp           # Stage your changes
$ git commit -m "challenge01: done"         # Commit your changes

$ git push -u origin challenge01            # Send changes to GitHub

To check your code, you can use the .scripts/check.py script or curl:

$ .scripts/check.py
Checking challenge01 program.cpp ...
  Result Success
    Time 0.02
   Score 6.00 / 6.00

$ curl -F source=@challenge01/program.cpp https://dredd.h4x0r.space/code/cse-30872-fa22/challenge01
{"result": "Success", "score": 6, "time": 0.0016994476318359375, "value": 6, "status": 0}

Pull Request

Once you have committed your work and pushed it to GitHub, remember to create a pull request and assign it to the appropriate teaching assistant from the Reading 01 TA List.