Tries
Tries are special types of trees that make searching and storing strings more efficient
class Trie:
def __init__(self):
self.ch = [None] * 26
self.end = False
def insert(self, word):
if not word:
self.end = True
return
cur = self
for ch in word:
if not cur.ch[ord(ch) - ord('a')]:
cur.ch[ord(ch) - ord('a')] = Trie()
cur = cur.ch[ord(ch) - ord('a')]
cur.end = True
def word(self, target):
if not target: return self.end
cur = self
for ch in target:
if not cur.ch[ord(ch) - ord('a')]: return False
cur = cur.ch[ord(ch) - ord('a')]
return cur.endRuntime analysis
Time complexity
Space complexity
Corner cases
Techniques
Preprocessing list of words
Using DFS for pattern matching
Inverting what is stored
Augmenting nodes to store words that end at the node
Last updated