Implement Trie (Prefix Tree) Problem

Implement Trie (Prefix Tree) Problem — ExecCode Hard DSA Practice

Solve the Implement Trie (Prefix Tree) problem on ExecCode. Free online hard DSA practice in Design. Write and run code in Java, C++, Python — no signup required to run.

Problem description

Implement a trie (prefix tree) with insert, search, and startsWith operations. A trie is a tree where each edge represents one character. insert(word) adds a word, search(word) returns true only if the exact word was inserted, and startsWith(prefix) returns true if any inserted word begins with that prefix.

Examples

Input ops = ["insert", "search", "search", "startsWith", "insert", "search"], vals = ["apple", "apple", "app", "app", "app", "app"]; Output [null, true, false, true, null, true]. Input ops = ["insert", "insert", "search", "search"], vals = ["cat", "car", "cat", "can"]; Output [null, null, true, false]. Input ops = ["insert", "startsWith", "startsWith"], vals = ["hello", "he", "wor"]; Output [null, true, false]

Constraints

1 <= word.length, prefix.length <= 2000 word and prefix consist only of lowercase English letters At most 3 * 10^4 calls in total will be made to insert, search, and startsWith

Practice Implement Trie (Prefix Tree) free on ExecCode. Browse DSA problems, topic map, and placement guides.