Skip to content

Files

Latest commit

 

History

History
26 lines (20 loc) · 2.2 KB

File metadata and controls

26 lines (20 loc) · 2.2 KB

Implement Prefix Tree (Trie) medium #javascript #blind75 #trie #data-structure

by Pawan Kumar @jsartisan

Take the Challenge

Implement a prefix tree (trie) that supports string insertion and search operations.

The PrefixTree class should support:

  • insert(word): Adds a word to the trie
  • search(word): Returns true if word exists in trie
  • startsWith(prefix): Returns true if any word starts with prefix

Constraints:

  • 1 ≤ word.length, prefix.length ≤ 1000
  • All strings contain only lowercase English letters

Examples:

const trie = new PrefixTree();
trie.insert("dog");
trie.search("dog");     // returns true
trie.search("do");      // returns false
trie.startsWith("do"); // returns true
trie.insert("do");
trie.search("do");      // returns true

Back Share your Solutions Check out Solutions