-
Notifications
You must be signed in to change notification settings - Fork 40
/
multi_search.py
84 lines (69 loc) · 2.03 KB
/
multi_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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/python
# Date: 2020-12-21
#
# Description:
# Given a string b and an array of smaller strings T, design a method to search
# b for each small string in T.
#
# Approach:
# - Build a trie and store all suffixes of bigger string in it - takes O(b^2)
# - Search for all smaller strings in trie, save all indexes that match
#
# Complexity:
# O(b^2 + kt)
# b = Length of bigger string
# t = Number of smaller strings in T
# k = Length of longest string in T
class TrieNode:
def __init__(self):
self.children = {} # Char to TrieNode
self.indexes = []
def insert_string(self, s, index):
self.indexes.append(index)
if len(s) == 0:
return
value = s[0]
if value in self.children:
child = self.children[value]
else:
child = TrieNode()
self.children[value] = child
child.insert_string(s[1:], index + 1)
def search(self, s):
if len(s) == 0:
return self.indexes
value = s[0]
if value in self.children:
child = self.children[value]
return child.search(s[1:])
return []
class Trie:
def __init__(self):
self.root = TrieNode()
def insert_string(self, s, index):
self.root.insert_string(s, index)
def search(self, s):
return self.root.search(s)
def create_trie_from_string(s):
trie = Trie()
for i in range(len(s)):
suffix = s[i:]
trie.insert_string(suffix, i)
return trie
def substract_value(locations, delta):
for i in range(len(locations)):
locations[i] = locations[i] - delta
def search_all(big, smalls):
lookup = {} # string to locations
tree = create_trie_from_string(big)
for small in smalls:
locations = tree.search(small)
substract_value(locations, len(small))
lookup[small] = locations
return lookup
def main():
b = 'mississippi'
T = ['is', 'ppi', 'hi', 'sis', 'i', 'ssippi']
print(search_all(b, T))
if __name__ == '__main__':
main()