1
+ // Runtime: 727 ms (Top 21.67%) | Memory: 101.6 MB (Top 6.67%)
1
2
var StreamChecker = function ( words ) {
2
3
function Trie ( ) {
3
4
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) {
6
7
}
7
8
this . root = new Trie ( ) ;
8
9
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
11
12
for ( const word of words ) {
12
13
let ptr = this . root ;
13
14
for ( const c of word ) {
@@ -17,46 +18,46 @@ var StreamChecker = function(words) {
17
18
ptr = ptr . next . get ( c ) ;
18
19
}
19
20
if ( ptr . id === - 1 ) {
20
- ptr . id = this . uniqueIds ++ ;
21
+ ptr . id = this . uniqueIds ++ ;
21
22
}
22
23
}
23
-
24
- //BFS traversal to build the automaton
24
+
25
+ //BFS traversal to build the automaton
25
26
const q = [ ] ;
26
27
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
28
29
node . suffixLink = this . root ;
29
30
q . push ( node ) ;
30
31
}
31
-
32
+
32
33
while ( q . length ) {
33
34
const curr = q . shift ( ) ;
34
35
for ( const [ c , node ] of curr . next ) {
35
-
36
- let ptr = curr . suffixLink ;
36
+
37
+ let ptr = curr . suffixLink ;
37
38
while ( ptr !== this . root && ! ptr . next . has ( c ) ) {
38
39
ptr = ptr . suffixLink ;
39
40
}
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
41
42
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
44
45
if ( node . suffixLink . id !== - 1 ) {
45
46
node . id = node . suffixLink . id ;
46
47
}
47
48
q . push ( node ) ;
48
49
}
49
50
}
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
51
52
this . queryPtr = this . root ;
52
53
} ;
53
54
54
55
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
56
57
while ( this . queryPtr !== this . root && ! this . queryPtr . next . has ( letter ) ) {
57
58
this . queryPtr = this . queryPtr . suffixLink ;
58
59
}
59
60
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
61
62
return this . queryPtr . id !== - 1 ;
62
- } ;
63
+ } ;
0 commit comments