Skip to content

Commit 73e4336

Browse files
committed
Runtime: 727 ms (Top 21.67%) | Memory: 101.6 MB (Top 6.67%)
1 parent 70cd7e8 commit 73e4336

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Runtime: 727 ms (Top 21.67%) | Memory: 101.6 MB (Top 6.67%)
12
var StreamChecker = function(words) {
23
function Trie() {
34
this.suffixLink = null; //this is where it will fallback to when a letter can't be matched
@@ -6,8 +7,8 @@ var StreamChecker = function(words) {
67
}
78
this.root = new Trie();
89
this.uniqueIds = 0; //used to count all the unique words discovered and as part of the Trie id system
9-
10-
//standard trie traversal but keeping track of new words via id system
10+
11+
//standard trie traversal but keeping track of new words via id system
1112
for (const word of words) {
1213
let ptr = this.root;
1314
for (const c of word) {
@@ -17,46 +18,46 @@ var StreamChecker = function(words) {
1718
ptr = ptr.next.get(c);
1819
}
1920
if (ptr.id === -1) {
20-
ptr.id = this.uniqueIds++;
21+
ptr.id = this.uniqueIds++;
2122
}
2223
}
23-
24-
//BFS traversal to build the automaton
24+
25+
//BFS traversal to build the automaton
2526
const q = [];
2627
for (const [c, node] of this.root.next) {
27-
//all first level children should point back to the root when a match to a character fails
28+
//all first level children should point back to the root when a match to a character fails
2829
node.suffixLink = this.root;
2930
q.push(node);
3031
}
31-
32+
3233
while (q.length) {
3334
const curr = q.shift();
3435
for (const [c, node] of curr.next) {
35-
36-
let ptr = curr.suffixLink;
36+
37+
let ptr = curr.suffixLink;
3738
while (ptr !== this.root && !ptr.next.has(c)) {
3839
ptr = ptr.suffixLink;
3940
}
40-
//find the next suffixLink if it matches the current character or fallback to the root
41+
//find the next suffixLink if it matches the current character or fallback to the root
4142
node.suffixLink = ptr.next.get(c) ?? this.root;
42-
43-
//if the current suffixLink happens to also be a word we should store its id to make it quick to find
43+
44+
//if the current suffixLink happens to also be a word we should store its id to make it quick to find
4445
if (node.suffixLink.id !== -1) {
4546
node.id = node.suffixLink.id;
4647
}
4748
q.push(node);
4849
}
4950
}
50-
//the query ptr will now track every new streamed character and use it to match
51+
//the query ptr will now track every new streamed character and use it to match
5152
this.queryPtr = this.root;
5253
};
5354

5455
StreamChecker.prototype.query = function(letter) {
55-
//the query ptr will now track every new streamed character and can be used to quickly find words
56+
//the query ptr will now track every new streamed character and can be used to quickly find words
5657
while (this.queryPtr !== this.root && !this.queryPtr.next.has(letter)) {
5758
this.queryPtr = this.queryPtr.suffixLink;
5859
}
5960
this.queryPtr = this.queryPtr.next.get(letter) ?? this.root;
60-
//if any word is found it will have an id that isn't -1
61+
//if any word is found it will have an id that isn't -1
6162
return this.queryPtr.id !== -1;
62-
};
63+
};

0 commit comments

Comments
 (0)