Solución
solution.tsTypeScript
function autocomplete(query: string, words: string[]): string[] {
if (!query) return words
const trie = {}
words.forEach(w => {
let temp = trie
for (const c of w) {
if (!temp[c]) temp[c] = {words: []}
temp[c]["words"].push(w)
temp = temp[c]
}
})
let temp = trie
for (const c of query) {
temp = temp[c]
if (!temp) return []
}
return temp["words"];
}
// No modificar: necesario para evaluar el resultado.
export { autocomplete };0respuestas