MCPcopy Create free account
hub / github.com/betomoedano/JavaScript-Coding-Interview-Questions / PrefixTrie

Class PrefixTrie

tries/multi-string-search.js:4–44  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2// MY SOLUTION
3////////////////////////////////////////////
4class PrefixTrie {
5 constructor(string) {
6 this.root = {};
7 this.lastSymbol = "*";
8 this.populateSuffixTrieFrom(string);
9 }
10
11 // Time O(n^2)
12 // Space O(n^2)
13 populateSuffixTrieFrom(string) {
14 for (let i = 0; i < string.length; i++) {
15 this.insertStringFrom(i, string);
16 }
17 }
18
19 insertStringFrom(i, string) {
20 let currentNode = this.root;
21
22 for (let j = i; j < string.length; j++) {
23 const letter = string[j];
24 if (!currentNode.hasOwnProperty(letter)) {
25 currentNode[letter] = {};
26 }
27 currentNode = currentNode[letter];
28 }
29 currentNode[this.lastSymbol] = true;
30 }
31
32 contains(string) {
33 let currentNode = this.root;
34 for (let i = 0; i < string.length; i++) {
35 const letter = string[i];
36 if (!currentNode.hasOwnProperty(letter)) {
37 return false;
38 }
39 currentNode = currentNode[letter];
40 }
41 // return currentNode.hasOwnProperty(this.lastSymbol);
42 return true;
43 }
44}
45// Time O(b^2 + ns)
46// Space O(b^2 + n)
47function multiStringSearch(bigString, smallStrings) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected