-
Notifications
You must be signed in to change notification settings - Fork 1
/
leetcode-1268-searchSuggestionsSystem-2.js
93 lines (84 loc) · 2.28 KB
/
leetcode-1268-searchSuggestionsSystem-2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* @param {string[]} products
* @param {string} searchWord
* @return {string[][]}
*/
// p: arr, str
// r: arr
// m
// |
// o
// / / \
// b n u
// | / \ |
// i e i s
//
// create a class TrieNode
class TrieNode {
constructor() {
this.isWord = false;
this.children = new Array(26).fill(null);
}
}
// a b c d e f g h i j k l m n o p
// children = [x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x]
// create a class Trie
class Trie {
constructor() {
this.root = new TrieNode();
this.alphabet = "abcdefghijklmnopqrstuvwxyz";
}
// insert words from arr to class Trie
insert(word) {
let c = this.root;
let a = this.alphabet;
for (let w of word) {
let i = a.indexOf(w);
!c.children[i] && (c.children[i] = new TrieNode());
c = c.children[i];
}
c.isWord = true;
}
// add words to result array following each prefix
getWordsStartWith(prefix) {
let c = this.root;
let a = this.alphabet;
const result = [];
for (let p of prefix) {
let i = a.indexOf(p);
if (!c.children[i]) return result;
c = c.children[i];
}
this.dfsGetWords(prefix, c, result);
return result;
}
// dfs to get words following prefixes
dfsGetWords(prefix, c, result) {
if (result.length === 3) return;
c.isWord && result.push(prefix);
let a = this.alphabet;
for (let i = 0; i < a.length; i++) {
c.children[i] && this.dfsGetWords(prefix + a[i], c.children[i], result);
}
}
}
var suggestedProducts = function (products, searchWord) {
// create new Trie
const trie = new Trie();
// insert words
for (let p of products) {
trie.insert(p);
}
const result = [];
let prefix = "";
// add words to result array
for (let w of searchWord) {
prefix += w;
result.push(trie.getWordsStartWith(prefix));
}
// return result array
return prefix;
};
const products = ["mobile", "mouse", "moneypot", "monitor", "mousepad"];
const searchWord = "mouse";
console.log(suggestedProducts(products, searchWord));