Implement Trie II (Prefix Tree)
The canonical "Implement Trie II (Prefix Tree)" problem defines a trie that supports insert, countWordsEqualTo, countWordsStartingWith, and erase, including duplicate word counts.
This repository uses a functional command-simulation contract. solve(arg1) receives an array of command strings and returns the sum of all query results.
Supported commands:
insert:<word>insertsworderase:<word>removes one occurrence if presentequal:<word>contributes the number of stored copies equal towordprefix:<prefix>contributes the number of stored words starting withprefix
Input: arg1 = ["insert:apple","insert:app","prefix:app","equal:apple","erase:app"]
Output: 3
arg1follows the functional execution schema- Commands are well-formed under the fixture contract
- Words and prefixes consist of lowercase English letters unless otherwise specified by fixtures
- Erasing a word not present leaves state unchanged
Related Problems
- implement-trie-prefix-tree
Companies Asked
- Amazon
- Microsoft
- Meta
Examples
Input: []
Output: 0
Input: ["insert:a","equal:a"]
Output: 1
Hints
Maintain pass-through and end counts at each trie node.
Decrement counts on erase only when the word exists.
`equal` uses the terminal count; `prefix` uses the pass-through count.
Related Problems
Implement Trie II (Prefix Tree)
The canonical "Implement Trie II (Prefix Tree)" problem defines a trie that supports `insert`, `countWordsEqualTo`, `countWordsStartingWith`, and `erase`, including duplicate word counts.