A dictionary service stores a bag of lowercase words (the same word may appear more than once, and each occurrence is stored separately). After all words are stored, you receive a series of prefix queries. For each query string p, report how many stored words have p as a prefix. A word is its own prefix, and the empty query (a blank line) matches every stored word.
n and q — the number of words to store and the number of queries.n lines: one word each, consisting only of lowercase English letters (a word may be repeated across lines).q lines: one query prefix each, consisting only of lowercase English letters (a query line may be empty, meaning the empty prefix).Print q lines. Line i is the number of stored words that begin with the i-th query prefix.
Example 1
Input
5 3 apple app apricot banana app app ap b
Expected
3 4 1
Explanation
The stored bag is [apple, app, apricot, banana, app]. Prefix `app` matches apple, app, app → 3. Prefix `ap` matches apple, app, apricot, app → 4. Prefix `b` matches banana → 1.
Example 2
Input
3 4 ab abc b abcd ab b z
Expected
0 2 1 0
Explanation
Prefix `abcd` matches no stored word → 0. Prefix `ab` matches ab and abc → 2. Prefix `b` matches b → 1. Prefix `z` matches nothing → 0.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →