Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Prefix Exists

Triesmedium

Implement `trieHasPrefix(words, prefix)` returning whether any stored word starts with `prefix`.

Sample tests

Input: trieHasPrefix(["cat","car"], "ca")
Output: true
Input: trieHasPrefix(["dog"], "ca")
Output: false

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Requiring an end marker would reject valid prefixes.

Learning resources

  • Wikipedia: Trie
Approach & explanation (try first)

Reaching the end of the prefix path means some word continues from there. O(prefix length).

Loading...
⌘/Ctrl + Enter

Run your code to see results.