Skip to content

Commit b1b0fac

Browse files
committed
add better solution for multi string search question
1 parent a5b72c0 commit b1b0fac

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

tries/multi-string-search.js

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
////////////////////////////////////////////
2+
// MY SOLUTION
3+
////////////////////////////////////////////
4+
class 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)
47+
function multiStringSearch(bigString, smallStrings) {
48+
const trie = new PrefixTrie(bigString);
49+
return smallStrings.map((string) => trie.contains(string));
50+
}
51+
52+
////////////////////////////////////////////
53+
// BETTER SOLUTION
54+
////////////////////////////////////////////
55+
class Trie {
56+
constructor() {
57+
this.root = {};
58+
this.endSymbol = "*";
59+
}
60+
61+
insert(string) {
62+
let currentNode = this.root;
63+
64+
for (let i = 0; i < string.length; i++) {
65+
const letter = string[i];
66+
if (!currentNode.hasOwnProperty(letter)) {
67+
currentNode[letter] = {};
68+
}
69+
currentNode = currentNode[letter];
70+
}
71+
currentNode[this.endSymbol] = string;
72+
}
73+
}
74+
// O(ns + bs) time | O(ns) space
75+
function multiStringSearch(bigString, smallStrings) {
76+
const trie = new Trie();
77+
for (const string of smallStrings) {
78+
trie.insert(string);
79+
}
80+
const containedStrings = {};
81+
for (let i = 0; i < bigString.length; i++) {
82+
findSmallStringsIn(bigString, i, trie, containedStrings);
83+
}
84+
return smallStrings.map((string) => string in containedStrings);
85+
}
86+
87+
function findSmallStringsIn(string, startIdx, trie, containedStrings) {
88+
let currentNode = trie.root;
89+
for (let i = startIdx; i < string.length; i++) {
90+
const currentChar = string[i];
91+
if (!currentNode.hasOwnProperty(currentChar)) break;
92+
currentNode = currentNode[currentChar];
93+
if (currentNode.hasOwnProperty(trie.endSymbol))
94+
containedStrings[currentNode[trie.endSymbol]] = true;
95+
}
96+
}

0 commit comments

Comments
 (0)