Understanding Prefix Word Counts
For each query prefix, count how many words start with that prefix. Return counts in query order.
How it works
A trie node represents a prefix and stores how many inserted words pass through it.
Insert words character by character, creating missing trie nodes. Increment the root and every visited prefix count. Follow each query’s characters and return its node count, or zero for a missing path.
Try an example
Input: words = ["apple", "app", "apt", "bat"], queries = ["ap", "app", "", "b", "z"]
Output: [3, 2, 4, 1, 0]
A trie node represents a prefix and stores how many inserted words pass through it.
Constraints
- At most 16 words and 16 queries, each containing 0–12 lowercase English letters.
- Repeated words count separately. An empty prefix matches every word, including an empty word.
Use the simulator
Change the input, then choose Visualize input. Use Next and Previous to inspect each decision, or Play to advance automatically. Compare approaches and their complexity. Choose Try yourself to write JavaScript, select My code and Visualize code to inspect its execution, or Run Tests to check results.
Execution happens in your browser. Drafts are saved locally; avoid entering secrets or sensitive information.