113. Dictionary with Flexible Search
Medium
Implement a dictionary that processes operations in order. An adicionar operation adds a word to the dictionary; a buscar operation checks whether any complete word matches the given pattern. In a pattern, the character . matches any lowercase letter. Words may be added more than once.
operacoes and valores have the same length. At each position, valores contains the word to add or the pattern to search for, according to the corresponding operation in operacoes. Process the operations in the given order and return an array with one result per operation: null for adicionar and a boolean for buscar.
Example 1
Input: operacoes = ["adicionar", "buscar", "buscar", "adicionar", "buscar"], valores = ["casa", "casa", "c.sa", "caso", "cas."]
Output: [null, true, true, null, true]
Explanation: Patterns containing a dot match complete words added to the dictionary.
Example 2
Input: operacoes = ["buscar", "adicionar", "buscar", "buscar", "adicionar", "buscar"], valores = ["mar", "mar", "ma", "m..", "lua", ".ua"]
Output: [false, null, false, true, null, true]
Explanation: A search considers only words already added and requires a matching length.
Constraints
- 0 <= operacoes.length <= 10^4
- valores.length == operacoes.length
- Each element of operacoes is "adicionar" or "buscar".
- Each element of valores contains 1 to 20 characters, all lowercase letters a through z, except that search patterns may also contain `.`.
- The total length of all elements in valores does not exceed 2 * 10^5.
TrieDFSDesign
Saved in this browser
Caso 1
operacoes =
["adicionar","buscar","buscar","adicionar","buscar"]
valores =
["casa","casa","c.sa","caso","cas."]
Expected =
[null,true,true,null,true]
Caso 2
operacoes =
["buscar","adicionar","buscar","buscar","adicionar","buscar"]
valores =
["mar","mar","ma","m..","lua",".ua"]
Expected =
[false,null,false,true,null,true]
Run checks these 2 cases. Submit runs all 11, including hidden ones.