Instructions
Write a class Trie that stores lowercase words and answers two kinds of questions about them. A trie (prefix tree) keeps words letter by letter, so words that start the same way share the same path.
The class has these methods:
Trie()creates an empty trie.insert(word)addswordto the trie. It returns nothing. Adding a word that is already there changes nothing.search(word)returnstrueifwordwas added before, andfalseif not. The whole word must match: if only"cart"was added,search("car")isfalse.startsWith(prefix)returnstrueif at least one added word begins withprefix, andfalseif not. A word counts as a prefix of itself, so after adding"cart",startsWith("cart")istrue.
How the input is shown
The test gives two lists of the same length. operations[i] is the name of a call and arguments[i] is the list of values for that call. The first call is always "Trie" (create the object). For example, operations = ["Trie", "insert", "search"] with arguments = [[], ["cat"], ["cat"]] means: create a trie, call insert("cat"), then call search("cat").
The expected output is a list with one entry per call: the value the call returns, or null for the constructor and for insert, which return nothing.
Hint: give each node a map from a letter to the next node, and a flag that says "a word ends here". search and startsWith walk the same path; only the last check is different.
Examples
Example 1
- Input:
- ["Trie","insert","search","search","startsWith","insert","search"], [[],["cart"],["car"],["cart"],["car"],["car"],["car"]]
- Output:
- [null,null,false,true,true,null,true]
After adding "cart", search("car") is false because "car" itself was never added, but startsWith("car") is true because "cart" begins with "car". Once "car" is added, search("car") becomes true.
Example 2
- Input:
- ["Trie","startsWith","search","insert","startsWith","startsWith","search"], [[],["a"],["a"],["hedge"],["h"],["hedge"],["hedges"]]
- Output:
- [null,false,false,null,true,true,false]
The trie is empty at first, so both questions about "a" are false. After adding "hedge", startsWith("h") and startsWith("hedge") are true. search("hedges") is false: it is longer than any added word.
Example 3
- Input:
- ["Trie","insert","insert","insert","insert","startsWith","search","startsWith","search"], [[],["bead"],["bed"],["bead"],["beach"],["bea"],["bea"],["bf"],["beach"]]
- Output:
- [null,null,null,null,null,true,false,false,true]
"bead" is added twice, which changes nothing. "bead" and "beach" both begin with "bea", so startsWith("bea") is true but search("bea") is false. No word begins with "bf".
Constraints
1 <= word.length, prefix.length <= 8wordandprefixcontain only lowercase lettersatoh.- At most
30calls toinsert,searchandstartsWithin total, after the constructor.
3 examples run when you press Run. Submit checks every hidden test too.
- Input:
- ["Trie","insert","search","search","startsWith","insert","search"], [[],["cart"],["car"],["cart"],["car"],["car"],["car"]]
- Expected:
- [null,null,false,true,true,null,true]
- Input:
- ["Trie","startsWith","search","insert","startsWith","startsWith","search"], [[],["a"],["a"],["hedge"],["h"],["hedge"],["hedges"]]
- Expected:
- [null,false,false,null,true,true,false]
- Input:
- ["Trie","insert","insert","insert","insert","startsWith","search","startsWith","search"], [[],["bead"],["bed"],["bead"],["beach"],["bea"],["bea"],["bf"],["beach"]]
- Expected:
- [null,null,null,null,null,true,false,false,true]
Run checks your code against the 3 examples. Submit checks it against all 73 tests and needs a free account. You can also try ideas in the free JavaScript or Python compiler.