[Algorithm] Search for matching words
Implement an autocomplete system. That is, given a query string
sand 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
deand 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 === ) {
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的更多相关文章
- [Algorithm] Search element in a circular sorted array
function findInCircularlySortedAry (ary = [], target) { ) { ; } ) { ] === target ? : -; } let , high ...
- 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 ...
- 广告召回 Query-Ad Matching
小结: 1.最为基础的召回链路就是要保证召回层的相关性,但是相关性高的广告并不一定具有很高的商业价值,所以开始尝试将一些商业化业务指标作为召回的依据 百度凤巢新一代广告召回系统--"莫比乌斯 ...
- (转)Awesome Courses
Awesome Courses Introduction There is a lot of hidden treasure lying within university pages scatte ...
- w3 parse a url
最新链接:https://www.w3.org/TR/html53/ 2.6 URLs — HTML5 li, dd li { margin: 1em 0; } dt, dfn { font-wei ...
- Computer Vision_18_Image Stitching: Image Alignment and Stitching A Tutorial——2006(book)
此部分是计算机视觉部分,主要侧重在底层特征提取,视频分析,跟踪,目标检测和识别方面等方面.对于自己不太熟悉的领域比如摄像机标定和立体视觉,仅仅列出上google上引用次数比较多的文献.有一些刚刚出版的 ...
- bash5.0参考手册
Bash Reference Manual a.summary-letter { text-decoration: none } blockquote.indentedblock { margin-r ...
- 算法编程Algos Programming
算法编程Algos Programming 不同算法的集合,用于编程比赛,如ACM ICPC. 算法按主题划分.大多数算法都可以从文件中按原样运行.每种算法都有一个参考问题,并对其时间和空间复杂度作了 ...
- 从头到尾彻底理解KMP
从头到尾彻底理解KMP 作者:July 时间:最初写于2011年12月,2014年7月21日晚10点 全部删除重写成此文,随后的半个多月不断反复改进. 1. 引言 本KMP原文最初写于2年多前的201 ...
随机推荐
- [LOJ2541][PKUWC2018]猎人杀(容斥+分治+FFT)
https://blog.csdn.net/Maxwei_wzj/article/details/80714129 n个二项式相乘可以用分治+FFT的方法,使用空间回收可以只开log个数组. #inc ...
- java线程系列文章之一(线程的安全性)
本文来自:高爽|Coder,原文地址:http://blog.csdn.net/ghsau/article/details/7421217,转载请注明. 当我们查看JDK API的时候,总会发现一些类 ...
- 特殊字符\u2028导致的Javascript脚本异常
这原本是个小错误,但排查花了不少时间,因此写下来和大家分享一下. 起因 通过Ajax动态从后台读取文章内容,并显示在页面上,加载到某篇文章的时候,报javascript语法错误,无法显示文章内容. A ...
- 用 Apache 发布 ASP.NET 网站
由于服务器需要发布 JSP .PHP.ASP.NET 几种网站进行测试,Apache 肯定是支持 JSP 和 PHP .鉴于 Apache 的开放精神 ,ASP.Net 应该也是支持的,于是乎 Go ...
- make and make bzImage
2.6内核 make = make bzImage + make modules 无非是改下Makefile而已 2.4 内核 01.make menuconfig 02.make dep 03.ma ...
- golang slice切片的原理以及内置函数cap, len
golang中slice(切片)是常用的类型, slice是对数组进行封装 package main import ( "fmt" "strconv") fun ...
- android 利用 aapt 解析 apk 得到应用名称 包名 版本号 权限等信息
在上传各大市场时发现 apk 上传后能自动解析出应用名称.包名.版本号.使用权限等信息,所以就研究了一下 1 直接解压 apk 解析 AndroidManifest.xml 是不行的,因为 apk ...
- Android图片加载框架最全解析(七),实现带进度的Glide图片加载功能
我们的Glide系列文章终于要进入收尾篇了.从我开始写这个系列的第一篇文章时,我就知道这会是一个很长的系列,只是没有想到竟然会写这么久. 在前面的六篇文章中,我们对Glide的方方面面都进行了学习,包 ...
- 《马上有招儿:PPT商务演示精选20讲(全彩) 》
<马上有招儿:PPT商务演示精选20讲(全彩) > 基本信息 作者:马建强 霍然 出版社:电子工业出版社 ISBN:9787121225123 上架时间:2014-3-11 出版日期 ...
- 为apache安装mod_wsgi的时候出现-fpic的问题
1.为了在apache里跑python项目,需要安装模块mod_wsgi 2.但是由于yum只支持python2.6,所以通过yum install mod_wsgi方式安装的mod_wsgi是pyt ...