forked from lupamo3-zz/coding-interview-gym
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Suffix_Trie_Construction.py
62 lines (47 loc) · 1.73 KB
/
Suffix_Trie_Construction.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
# https://www.algoexpert.io/questions/Suffix%20Trie%20Construction
"""
The way we can build our Suffix-Trie is to use a bunch of hash tables or dictionaries. Where every node in the suffix
tree is gonna be a Key in a dictionary, pointing to another dictionary/HashTable (the value of the key will store of
the next node's dictionary directly so basically, it will be a nested dictionary of dictionary). And all the values in
the dictionary, will be the other nodes in the tree whose keys will be a specific letter that comes after the previous
letter, and that points to another hash table, and so on and so forth.
Sample input (for creation):"babc"
Sample output (for creation):
{
"c": {"*": True},
"b": {
{"c": {"*": True},
{"a": {"b":
{"c": {"*": True}}},
},
"a": {
"b":
{"c": {"*": True}}},
}
"""
class SuffixTrie:
def __init__(self, string):
self.root ={}
self.end_symbol = "*"
self.populate_suffix_trie_from(string)
def populate_suffix_trie_from(self, string):
for i in range(len(string)):
self.insert_substring_starting_at(i, string)
def insert_substring_starting_at(self, i, string):
node = self.root
for j in range(i, len(string)):
letter = string[j]
if letter not in node:
node[letter] = {}
node = node[letter]
node[self.end_symbol] = True
def contains(self, string):
node = self.root
for letter in string:
if letter not in string:
return False
node = node[letter]
return self.end_symbol in node
st = SuffixTrie("babc")
contains = st.contains("bc")
print("Restlt: ", contains)