Dodomeki
Sign in

49. Word Counter at the Desk

Easy

A desk receives words and prefix queries. Each operation in operacoes corresponds to the value at the same position in valores: "inserir" adds the given word, and "contar" asks how many inserted words start with the given prefix.

Return an array with one position for each operation. Put null at insertion positions and the matching count at query positions. Repeated insertions count separately. A query with an empty prefix counts every inserted word, including empty words.

Example 1
Input: operacoes = ["inserir", "inserir", "contar", "inserir", "contar"], valores = ["casa", "casaco", "cas", "casamento", "cas"]
Output: [null, null, 2, null, 3]
Explanation: The first query finds two words starting with "cas"; after another insertion, the next query finds three.
Example 2
Input: operacoes = ["contar", "inserir", "inserir", "contar", "contar"], valores = ["", "", "luz", "", "lu"]
Output: [0, null, null, 2, 1]
Explanation: The empty-prefix query counts every word, including the inserted empty word.
Constraints
  • 1 <= operacoes.length <= 10^5
  • `operacoes.length` == `valores.length`
  • Each element of `operacoes` is `"inserir"` or `"contar"`.
  • Each element of `valores` contains only lowercase letters from `a` to `z` and has length from 0 to 10^3; the sum of the lengths does not exceed 2 * 10^5.
TrieStringsDesign
Caso 1
operacoes =
["inserir","inserir","contar","inserir","contar"]
valores =
["casa","casaco","cas","casamento","cas"]
Expected =
[null,null,2,null,3]
Caso 2
operacoes =
["contar","inserir","inserir","contar","contar"]
valores =
["","","luz","","lu"]
Expected =
[0,null,null,2,1]

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