The home where you level up

Save my spot

Suffix Tree

Term side of the Card.
Topic: Tree
Unit title: Suffix Tree
Categories: Data structure
Explanation side of the Card

Explanation: The specialized tree that stores all suffixes of a string, enabling efficient substring and pattern matching. Known for rapid text searching with linear space and time complexity.

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.

The home where you level up

Daily 3-minute lessons to understand your AI's code.

No spam — just launch news.
Unsubscribe anytime. · Privacy Policy

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy