Dodomeki
Sign in
← Challenges
‹›

116. Prefix catalog

Hard

Implement a word catalog, initially empty, and process the operations in order. add inserts a word, without duplicating entries; remove deletes it if it is registered; search checks whether the whole word exists; prefixCount returns how many registered words start with the given prefix. Return a list with the result of each search or prefixCount operation, in the order they occur. For prefixCount, the empty prefix represents all words.

Example 1
Input: operacoes = [["add", "casa"], ["add", "casulo"], ["search", "casa"], ["prefixCount", "cas"], ["remove", "casa"], ["prefixCount", "cas"]]
Output: [true, 2, 1]
Explanation: The queries return true for casa; first there are two words with prefix cas, then after removal there is one.
Example 2
Input: operacoes = [["add", "uva"], ["add", "uva"], ["prefixCount", ""], ["remove", "uva"], ["search", "uva"]]
Output: [1, false]
Constraints
  • 0 <= operacoes.length <= 10000
  • Each operation is a pair [acao, palavra].
  • acao is add, remove, search or prefixCount.
  • Words and prefixes contain only lowercase letters a to z and are up to 100 characters long.
TrieStringsDesign
Caso 1
operacoes =
[["add","casa"],["add","casulo"],["search","casa"],["prefixCount","cas"],["remove","casa"],["prefixCount","cas"]]
Expected =
[true,2,1]
Caso 2
operacoes =
[["add","uva"],["add","uva"],["prefixCount",""],["remove","uva"],["search","uva"]]
Expected =
[1,false]

Run checks these 2 cases. Submit runs all 6, including hidden ones.