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. 【SPFA判断负环】BZOJ1715- [Usaco2006 Dec]Wormholes 虫洞

    [题目大意] 判断一张图中是否存在负环. [思路] dfs版SPFA. #include<bits/stdc++.h> using namespace std; struct edge { ...

  2. vijos p1883

    题意: 有些东西就如同月光的魔法一般. Luke是爱着vijos的.他想为自己心爱的东西画些什么. 就画N个圆吧.把它们的圆心都固定在x轴上. 圆与圆.为了爱,两两不能相交.为了爱,它们可以互相贴在一 ...

  3. bzoj 3275 最小割

    给你一堆东西,叫你选一些东西出来,使得价值最大,要求选出的东西集合中的任意a,b满足性质p. 可以考虑: 1.拟阵? 2.二分图? 这道题由于数学硬伤,不知道不存在两条直角边是奇数,斜边是整数的直角三 ...

  4. 华为S5300系列升级固件S5300SI-V200R001C00SPC300.cc

    附带web,V200版本的第一个固件. 附件: 链接:https://pan.baidu.com/s/1QyXIIVho9AkhxUFYJYAkcw  密码:gaxm

  5. C++中使用REST操作

    REST现在是非常流行的一种接口了,但对于C++这种古董语言来说,用起来并不很方便.无论是json操作还是http交互,用起来都比较麻烦. 如果你需要在c++中使用rest操作时,不妨试一下微软的cp ...

  6. [MySql]windows下设置mysql默认编码

    摘要 在安装好mysql的时候,如果新建数据库或者表默认的编码为latin1,如果这时候插入中文时,出出现类似下面的乱码的问题. SQLException: Incorrect string valu ...

  7. pytest文档19-doctest测试框架

    前言 doctest从字面意思上看,那就是文档测试.doctest是python里面自带的一个模块,它实际上是单元测试的一种. 官方解释:doctest 模块会搜索那些看起来像交互式会话的 Pytho ...

  8. Selenium2+python自动化48-登录方法(参数化)

    前言 登录这个场景在写用例的时候经常会有,我们可以把登录封装成一个方法,然后把账号和密码参数化,这样以后用的登录的时候,只需调用这个方法就行了 一.登录方法 1.把输入账号.输入密码.点击登录按钮三个 ...

  9. [活动已结束]《深入理解Android:Wi-Fi、NFC和GPS卷》CSDN社区活动

    今天有一个CSDN社区活动,解答关于Android系统的学习.认识.开发等方面的问题,并就<深入理解Android:Wi-Fi.NFC和GPS卷>一书为大家答疑解惑,了解Android F ...

  10. 把NDK的工具链提取出来单独使用

    独立toolchain 把NDK压缩包解压到系统,如/mnt目录下,后在/mnt目录下建立文件夹my_ndk_toolchain,然后再/mnt目录下执行以下命令:/mnt/android-ndk-r ...