Frontend101 logoFrontend101
← Problems
#13MediumAlgorithmic coding

13. Implement a Trie (Prefix Tree)

JavaScriptOOP

Implement a Trie data structure with insert(word), search(word), and startsWith(prefix) methods.

Example 1:
const trie = new Trie();
trie.insert("apple");
trie.search("apple");   // true
trie.search("app");     // false
trie.startsWith("app"); // true
solution.js
Supports insert, search, and startsWith
(() => {
          const t = new Trie();
          t.insert("apple");
          expect(t.search("apple")).toBe(true);
          expect(t.search("app")).toBe(false);
          expect(t.startsWith("app")).toBe(true);
        })()