forked from lupamo3-zz/coding-interview-gym
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Multi_String_Search.py
66 lines (56 loc) · 2.03 KB
/
Multi_String_Search.py
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
# Solution 02
# O(b^2 + ns) time | O(b^2 + n) space
def multiStringSearch(bigString, smallStrings):
modifiedSuffixTrie = ModifiedSuffixTrie(bigString)
return [modifiedSuffixTrie.conntains(string) for string in smallStrings]
class ModifiedSuffixTrie:
def __init__(self, string):
self.root = {}
self.populateModifiedSuffixTrieFrom(string)
def populateModifiedSuffixTrieFrom(self, string):
for i in range(len(string)):
self.insertSubstringStartinngAt(i, string)
def insertSubstringStartinngAt(self, idx, string):
node = self.root
for j in range(idx, len(string)):
letter = string[j]
if letter not in node:
node[letter] = {}
node = node[letter]
def conntains(self, string):
node = self.root
for letter in string:
if letter not in node:
return False
node = node[letter]
return True
# Solution 03
# O(bs + ns) time | O(ns) space
def multiStringSearch(bigString, smallStrings):
trie = Trie()
for string in smallStrings:
trie.insert(string)
containedString = {}
for i in range(len(bigString)):
findSmallStringsIn(bigString, i, trie, containedString)
return [string in containedString for string in smallStrings]
def findSmallStringsIn(string, startIdx, trie, containedStringns):
currentNode = trie.root
for i in range(startIdx, len(string)):
currentChar = string[i]
if currentChar not in currentNode:
break
currentNode = currentNode[currentChar]
if trie.endSymbol in currentNode:
containedStringns[currentNode[trie.endSymbol]] = True
class Trie:
def __init__(self):
self.root = {}
self.endSymbol = '*'
def insert(self, string):
current = self.root
for i in range(len(string)):
if string[i] not in current:
current[string[i]] = {}
current = current[string[i]]
current[self.endSymbol] = string