Backend Engineering
Mid
programming
LeetCode #208 - Implement Trie (Prefix Tree)
Implement a trie with insert, search, and startsWith methods. A trie (pronounced as 'try') is a special data structure used to store a dynamic set or associative array where the keys are usually strings. It is a tree-like data structure where each node represents a character of the string.Example:
Input:
"insert" - "apple"
"search" - "apple"
"search" - "app"
"startsWith" - "app"
"insert" - "app"
"search" - "app" Output:
true
false
true
true Constraints:
1. All inputs are lowercase letters a-z.
2. The inputs are strings of at most 200 characters.
3. The maximum number of inserts in the trie is 10000.
Input:
"insert" - "apple"
"search" - "apple"
"search" - "app"
"startsWith" - "app"
"insert" - "app"
"search" - "app" Output:
true
false
true
true Constraints:
1. All inputs are lowercase letters a-z.
2. The inputs are strings of at most 200 characters.
3. The maximum number of inserts in the trie is 10000.
Suggested Answer