928. Count Words with Given Prefix in Trie

MediumTreesTrieHashCounting

Given a list of words and a list of query prefixes, return for each prefix the number of words that start with it. The input is JSON {words, prefixes}, and the output is an array of counts (one per prefix, in order).

Input: JSON {words, prefixes}.

Output: Array — the count of matching words per prefix.

Examples

Example 1
Input: {"words":["apple","app","apply","banana"],"prefixes":["app","ban","z"]}
Output: [3,1,0]
Explanation: Counts for each prefix.
Example 2
Input: {"words":["a","ab","abc"],"prefixes":["a","ab","abc","abcd"]}
Output: [3,2,1,0]
Explanation: Nested prefixes.
Example 3
Input: {"words":["x"],"prefixes":["x","y"]}
Output: [1,0]
Explanation: Single word.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →