Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Count Words With Prefix

Triesmedium

Implement `countWordsWithPrefix(words, prefix)` returning how many stored words start with `prefix`.

Sample tests

Input: countWordsWithPrefix(["cat","car","dog"], "ca")
Output: 2
Input: countWordsWithPrefix(["a","ab","abc"], "ab")
Output: 2

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Counting trie nodes instead of word-end markers.

Learning resources

  • Wikipedia: Trie
Approach & explanation (try first)

Summing the end counts in the subtree under the prefix node gives the number of matching words.

Loading...
⌘/Ctrl + Enter

Run your code to see results.