Back to DSA

Implement Trie (Prefix Tree)

medium
Acceptance: 53%
TrieDesignString

A trie (prefix tree) is a specialised tree for storing and looking up strings efficiently. Build a Trie class with three operations: insert(word) adds a word, search(word) returns true only if the exact word exists, and startsWith(prefix) returns true if any stored word begins with the given prefix.

Examples

Example 1:
Input:Trie(), insert('hello'), search('hello'), search('hell'), startsWith('hel'), insert('hell'), search('hell')
Output:[null,null,true,false,true,null,true]

Hints

00:00
1234567