[Algorithm] Search for matching words
Implement an autocomplete system. That is, given a query string
s
and a set of all possible query strings, return all strings in the set that have s as a prefix.For example, given the query string
de
and the set of strings [dog
,deer
,deal
], return [deer
,deal
].Hint: Try preprocessing the dictionary into a more efficient data structure to speed up queries.
It is using the Trie data stucture, check thist post;
The only changes is that when we search for words, it is partially matched + whole word match.
Which means if we search 'de', it should return [deer, deal],
if we search 'deer', it should return [deer],
if we search 'deerr', it should return [].
function Node () { return { keys: new Map(), isEnd: false } } function Trie () { return { root: new Node(), // Add words into the tree // Recursive add(word, node = this.root) { // If there is no word, means it reach the end if (word.length === 0) { node.isEnd = true; return; } const [head, ...rest] = word; // If char is not set, then create a new Node to hold char // Otherwise, continue if (!node.keys.has(head)) { node.keys.set(head, new Node(head)); } // Continue with three this.add(rest, node.keys.get(head)); }, // Print all the words in the tree print () { let words = []; // Helper function, Recursive function search (node = this.root, string = '') { // Make sure we have keys if (node.keys.size !== 0) { for (let key of node.keys.keys()) { // update node, update string search(node.keys.get(key), string.concat(key)); } // if reach the end, push to the words if (node.isEnd) { words.push(string); } } else { // If there is no keys, then push the avaialbe string to the words string.length > 0 ? words.push(string) : null; } } search(this.root, ''); return words.length > 0 ? words : null; }, // Find the words based on input find (input) { let words = []; function search(node = this.root, string = '', input) { if (node.keys.size !== 0) { for (let key of node.keys.keys()) { // if the key is the same as input[0] // becasue we want the whole word, so continue with current trees if (key === input[0] || input.length === 0) { let rest = input.length === 0 ? '': input.substr(1); search(node.keys.get(key), string.concat(key), rest); } } } else { // In case of input is 'cattt', then return undefined if (input.length !== 0) { return []; } string.length > 0 ? words.push(string) : []; } } search(this.root, '', input); return words.length > 0 ? words : []; } } } function searchWords () { const data = ['dog', 'dollar', 'cat', 'drag']; const trie = new Trie(); data.forEach(d => trie.add(d)); console.log(trie.find('do')); // [ 'dog', 'dollar' ] console.log(trie.print()); // [ 'dog', 'dollar', 'drag', 'cat' ] } searchWords();
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2017-03-17 [Angular] USING ZONES IN ANGULAR FOR BETTER PERFORMANCE
2017-03-17 [TypeScript] Using ES6 and ESNext with TypeScript
2016-03-17 [AngularJS] Angular 1.5 multiple transclude
2016-03-17 [RxJS] Creating Observable From Scratch
2015-03-17 [AngularJS] Introduction to angular-formly