Problem
Build a trie, a tree that stores words letter by letter.
insert adds a word to the trie.
search returns true if that exact word was added, and false if not.
startsWith returns true if any added word begins with the given prefix.
Examples
Example 1:
Input: calls = ["Trie","insert","search","search","startsWith","insert","search"], args = [[],["apple"],["apple"],["app"],["app"],["app"],["app"]]
Output: [null,null,true,false,true,null,true]
Explanation: each test makes a Trie and then the calls, in order, each with its arguments: insert("apple"), search("apple"), search("app"), startsWith("app"), insert("app"), search("app"). Output has an answer for every call, null where a call returns nothing: app is a prefix of apple but not a word until it is inserted.
Example 2:
Input: calls = ["Trie","insert","insert","search","startsWith","insert","search","search"], args = [[],["card"],["cat"],["car"],["car"],["car"],["car"],["cart"]]
Output: [null,null,null,false,true,null,true,false]
Explanation: car is only a prefix until it is inserted; cart falls off the trie after car.Constraints
1 <= word.length, prefix.length <= 2000
word and prefix consist only of lowercase English letters.
At most 3 * 10^4 calls in total are made to insert, search and startsWith.
How to think about it
1. A Node Per Letter Optimal
Intuition
A trie stores words as paths of letters down from a root: each node holds its children by letter, and a mark saying a word ends there. Words that share a prefix share its nodes. Both queries walk the same path, letter by letter, and stop when a letter has no child; the mark is the only difference between them: startsWith succeeds when the walk arrives, search only when the node it arrives at is marked.
Algorithm
1. A TrieNode holds children, a map from a letter to the next node, and is_end (Java: isEnd), false until a word ends there. The Trie holds a root node.
2. insert: from the root, for each letter, make the child if it is missing and step down to it; mark the last node.
3. walk: from the root, for each letter, return nothing (None, null) if the child is missing, else step down; return the node it arrives at.
4. search: walk the word, and answer whether it arrived at a marked node. startsWith: walk the prefix, and answer whether it arrived.
Time & Space
Time O(L) for each call, L the length of its word or prefix: one step down a letter. Space O(N) for the nodes, N the number of letters inserted at most: a word adds one node for each letter past the part of it already stored.
2. 26 Slots a Node
Intuition
The same trie, its children held the other common way. Every word is lowercase English letters, so a node can keep an array of 26 children, one slot a letter, and find the child for a letter at the letter's distance from 'a'; an empty slot is a letter no stored word takes there. The walk and the mark are the first approach's: insert fills the empty slots on its path and marks the last node, and search and startsWith walk the same path, startsWith succeeding when it arrives and search only at a marked node. A slot is read by its index where the first approach looks a letter up in a map; the price is that every node carries all 26 slots, used or not.
Algorithm
1. A TrieNode holds children, an array of 26 slots, all empty at first, and is_end (Java: isEnd), false until a word ends there. The Trie holds a root node.
2. insert: from the root, for each letter, take its index (the letter minus 'a'); fill that slot with a new node if it is empty, and step down to it; mark the last node.
3. walk: from the root, for each letter, step down to the slot at its index, and return nothing (None, null) if the slot is empty; return the node it arrives at.
4. search: walk the word, and answer whether it arrived at a marked node. startsWith: walk the prefix, and answer whether it arrived.
Time & Space
Time O(L) for each call, L the length of its word or prefix: one slot read a letter. Space O(26 * N) for the nodes, N the number of letters inserted at most: each node holds 26 slots, however few of them it uses.