-
Notifications
You must be signed in to change notification settings - Fork 1
/
leetcode-1268-searchSuggestionsSystem-3.js
105 lines (96 loc) · 3.27 KB
/
leetcode-1268-searchSuggestionsSystem-3.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
94
95
96
97
98
99
100
101
102
103
104
105
/**
* @param {string[]} products
* @param {string} searchWord
* @return {string[][]}
*/
// The time complexity of building a trie for the given problem is O(P),
// where P is the sum of the lengths of all the strings in the products array.
// This is because each string in the products array needs to be inserted into
// the trie and each insertion takes O(L) time, where L is the length of the string.
// The space complexity of the trie solution is O(26^L * P),
// where L is the length of the longest string in the products array
// and P is the total number of characters in all the strings in the products array.
// This is because each node in the trie can have 26 children,
// one for each letter of the alphabet, and each node requires O(1) space.
// ["mobile","mouse","moneypot","monitor","mousepad"]
// root
// | [-,-,-,-,-,-,-,-,-,-,-,-,*,-,-,-,-,-,-,-,-,-,-,-,-,-,]
// m
// | [-,-,-,-,-,-,-,-,-,-,-,-,-,-,*,-,-,-,-,-,-,-,-,-,-,-,]
// o
// / \ \ [-,*,-,-,-,-,-,-,-,-,-,-,-,*,-,-,-,-,-,-,*,-,-,-,-,-,]
// b u n
// | | | \
// i s e i
// | | | |
// l e* y t
// | | | |
// e* p p o
//
// abcdefghijklmnopqrstuvwxyz
//
// create TrieNode
// [-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,]
class TrieNode {
constructor() {
this.children = new Array(26);
this.isWord = false;
}
}
// create Trie from TrieNode
class Trie {
constructor() {
this.alphabet = "abcdefghijklmnopqrstuvwxyz";
this.root = new TrieNode();
}
// insert all products into the Trie
insert(word) {
let r = this.root;
let a = this.alphabet;
for (let w of word) {
let i = a.indexOf(w);
!r.children[i] && (r.children[i] = new TrieNode());
r = r.children[i];
}
r.isWord = true;
}
// return an array for each prefix;
wordsStartWith(prefix) {
let r = this.root;
let a = this.alphabet;
const result = [];
// go to the last char of prefix
for (let p of prefix) {
let i = a.indexOf(p);
// check if prefix has words
if (!r.children[i]) return result;
r = r.children[i];
}
this.dfs(prefix, r, result);
return result;
}
// dfs through the Trie and mark isWord: true;
dfs(prefix, r, result) {
if (result.length === 3) return;
r.isWord && result.push(prefix);
const a = this.alphabet;
for (let i = 0; i < r.children.length; i++) {
r.children[i] && this.dfs(prefix + a[i], r.children[i], result);
}
}
}
var suggestedProducts = function (products, searchWord) {
const results = [];
const trie = new Trie();
// insert products
for (let product of products) {
trie.insert(product);
}
// add the returned array to result;
let prefix = "";
for (let w of searchWord) {
prefix += w;
results.push(trie.wordsStartWith(prefix));
}
return results;
};