Tries
Tries are special types of trees that make searching and storing strings more efficient
Runtime analysis
Time complexity
m
is the length of the string
Search:
Insert:
Remove:
Space complexity
where is the number of strings and is the length of the longest string
Corner cases
Searching for a string in an empty trie
Inserting empty strings into a trie
Techniques
Preprocessing list of words
Use a trie to store a list of words to improve the efficiency for searching for a word of length k
among n
words
It takes only over
Using DFS for pattern matching
Pattern matching with values like *
and .
can be performed using DFS where branching is done to all valid "next" characters when these characters are encountered, otherwise normal traversal is used
Inverting what is stored
Some problems will use tries by storing a different set of data available, try out various inputs to see what works best
See problems like Word Search 2 for reference
Augmenting nodes to store words that end at the node
This is a slight optimization to reduce the need to accumulate the strings as the Trie traversal is occurring
See problems like Word Search 2 for reference
Last updated