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 [dogdeerdeal], return [deerdeal].

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 === ) {
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 !== ) {
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 > ? words.push(string) : null;
}
} search(this.root, '');
return words.length > ? words : null;
},
// Find the words based on input
find (input) {
let words = []; function search(node = this.root, string = '', input) { if (node.keys.size !== ) {
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[] || input.length === ) {
let rest = input.length === ? '': input.substr();
search(node.keys.get(key), string.concat(key), rest);
}
}
} else {
// In case of input is 'cattt', then return undefined
if (input.length !== ) {
return [];
}
string.length > ? words.push(string) : [];
}
} search(this.root, '', input); return words.length > ? 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();

[Algorithm] Search for matching words的更多相关文章

  1. [Algorithm] Search element in a circular sorted array

    function findInCircularlySortedAry (ary = [], target) { ) { ; } ) { ] === target ? : -; } let , high ...

  2. Deep Dive into Neo4j 3.5 Full Text Search

    In this blog we will go over the Full Text Search capabilities available in the latest major release ...

  3. 广告召回 Query-Ad Matching

    小结: 1.最为基础的召回链路就是要保证召回层的相关性,但是相关性高的广告并不一定具有很高的商业价值,所以开始尝试将一些商业化业务指标作为召回的依据 百度凤巢新一代广告召回系统--"莫比乌斯 ...

  4. (转)Awesome Courses

    Awesome Courses  Introduction There is a lot of hidden treasure lying within university pages scatte ...

  5. w3 parse a url

     最新链接:https://www.w3.org/TR/html53/ 2.6 URLs — HTML5 li, dd li { margin: 1em 0; } dt, dfn { font-wei ...

  6. Computer Vision_18_Image Stitching: Image Alignment and Stitching A Tutorial——2006(book)

    此部分是计算机视觉部分,主要侧重在底层特征提取,视频分析,跟踪,目标检测和识别方面等方面.对于自己不太熟悉的领域比如摄像机标定和立体视觉,仅仅列出上google上引用次数比较多的文献.有一些刚刚出版的 ...

  7. bash5.0参考手册

    Bash Reference Manual a.summary-letter { text-decoration: none } blockquote.indentedblock { margin-r ...

  8. 算法编程Algos Programming

    算法编程Algos Programming 不同算法的集合,用于编程比赛,如ACM ICPC. 算法按主题划分.大多数算法都可以从文件中按原样运行.每种算法都有一个参考问题,并对其时间和空间复杂度作了 ...

  9. 从头到尾彻底理解KMP

    从头到尾彻底理解KMP 作者:July 时间:最初写于2011年12月,2014年7月21日晚10点 全部删除重写成此文,随后的半个多月不断反复改进. 1. 引言 本KMP原文最初写于2年多前的201 ...

随机推荐

  1. 【HackerRank Week of Code 31】Colliding Circles

    https://www.hackerrank.com/contests/w31/challenges/colliding-circles/problem 设E(n)为序列长度为n时的期望值. \[ \ ...

  2. POJ2155【二维树状数组,区间修改,点查询?】【又被输入输出坑】

    这题反反复复,到现在才过. 这道题就是树状数组的逆用,用于修改区间内容,查询点的值. 如果单纯就这个奇偶数来判的话,似乎这个思路比较好理解. 看了一下国家集训队论文(囧),<关于0与1在信息学奥 ...

  3. MAX II Device Compatibility with 5.0-V CMOS Devices

    http://www.altera.com/literature/hb/max2/max2_mii51009.pdf The open-drain pin never drives high, onl ...

  4. Java工程师成神之路 转

      一.基础篇 1.1 JVM 1.1.1. Java内存模型,Java内存管理,Java堆和栈,垃圾回收 http://www.jcp.org/en/jsr/detail?id=133 http:/ ...

  5. OpenCV 机器学习之 支持向量机的使用方法实例

    用支持向量机进行文理科生的分类,根据的特征主要是 数学成绩与语文成绩,这两个特征都服从高斯分布 程序代码例如以下: 分类结果:

  6. 进程内COM与进程外COM

    1.进程内和进程外Com COM/DCOM 组件可以在DLL 或EXE 文档中实现. 其中在 DLL 中实现的COM/DCOM组件称为 In-Process Server,因为这些组件是加载到使用它们 ...

  7. Google Admob广告Android全攻略1

    一.登录Google AdMob中文官网:http://www.google.cn/ads/admob/   ,注册帐号. 1.点击注册AdMob帐号 2.进入Google帐号注册页面,因为要创建一个 ...

  8. java、js中实现无限层级的树形结构(类似递归)

    js中: var zNodes=[ {id:0,pId:-1,name:"Aaaa"}, {id:1,pId:0,name:"A"}, {id:11,pId:1 ...

  9. JavaScript 面向对象编程之一

    一:Class and private And public JS 中的类以 function 进行声明,同时 JS 也支持声明私有 private 和公有 public 成员,只不过跟 C# 不一样 ...

  10. wifiphisher使用介绍

    1.github地址:https://github.com/sophron/wifiphisher 2.需要安装在kali linux下面 3.需要两个无线网卡 4.安装方法是使用介绍,参考githu ...