Suffix Tree
Common mistakes
- Building it by inserting every suffix into a plain trie, which takes O(n²) time and space.
- Forgetting a unique end marker such as '$', so some suffixes end in the middle of an edge.
Interview questions
What is the time complexity of building a suffix tree?
Ukkonen's algorithm builds it in O(n) for a fixed alphabet. A substring search then takes O(m), where m is the pattern length.
Suffix tree vs suffix array: which should I use?
A suffix array stores only the sorted suffix positions, so it uses much less memory. It answers the same queries with binary search, at a small extra time cost.
Suffix tree vs trie: what is the difference?
A trie stores a set of separate words. A suffix tree is a compressed trie of all suffixes of one text, used to search for substrings inside it.
