[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 ...
随机推荐
- 牛可乐发红包脱单OI赛 C 小可爱表白
打个暴力查一下OEIS,5min做完 出题人一开始把式子打错了,一开始的式子的结果为$n * (n + 3) * 2^{n - 3}$ 我们考虑化式子 首先考虑 $\sum\limits_{j = 1 ...
- [BZOJ5338][TJOI2018]xor(可持久化Trie)
可持久化Trie模板题. 建两种可持久化Trie,每个点两棵,一棵对DFS求前缀和,一棵对祖先求前缀和. 或者树剖,不好写多少还多个log. #include<cstdio> #inclu ...
- 新的起点 Entry KINGSOFT
夜里,陪宝宝睡了会,呃岁月转变,变化里,不经意间加入了kingsoft. 呃,第一天所以算是一个起点或是一个开始.遇到些琐事,Slow network,oa Account login O(∩_∩)O ...
- pear中几个实用的xml代码库
1.XML_Beautifier 用于将一段排版凌乱的XML文档美化 <?php require_once "XML/Beautifier.php"; $fmt = new ...
- UVALive 4426 Blast the Enemy! 计算几何求重心
D - Blast the Enemy! Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Subm ...
- ROS知识(6)----卸载ROS系统
步骤方法: 1.首先卸载包 sudo apt-get purge ros-* 2.然后卸载依赖包 sudo apt-get autoremove
- EXC_BAD_ACCESS(code=2,address=0xcc 异常解决 及 建议不要在子线程中刷新界面
iOS 上不建议在非主线程进行UI操作,在非主线程进行UI操作有很大几率会导致程序崩溃,或者出现预期之外的效果. 我开始不知道这一点,在子线程中进行了弹窗操作,结果程序就出问题了! 报的错误是(EXC ...
- Android Gradle Plugin指南(五)——Build Variants(构建变种版本号)
原文地址:http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Variants 6. Build Vari ...
- Android之应用内部实现国际化
这篇文章也提供了应用内部转换语言的方法: http://blog.csdn.net/sodino/article/details/6596709 .1和2的方法是一样的,然而还是会调整了手机的语言设置 ...
- 操作系统Day1地址空间与地址生成
1.地址空间分成(1)物理地址空间 (2)逻辑地址空间二者之间的关系:*逻辑地址空间的生成:程序——>汇编-->linker——>loader*物理地址的生成:内存的逻辑地址空间会有 ...