Data Structures19 sections · 729 units
Open in Course

Suffix Tries and Suffix Trees

Pattern matching powerhouse

A suffix trie contains all suffixes of a string.

For "banana":

  • banana
  • anana
  • nana
  • ana
  • na
  • a

This approach enables O(P)O(P) substring search (where PP is pattern length): a pattern exists in the string if and only if it's a prefix of some suffix.

Problem: suffix trie has O(n2)O(n^2) nodes for a string of length nn. Suffix trees compress the suffix trie, reducing space to O(n)O(n).

They enable:

  • Substring search in O(P)O(P)
  • Finding longest repeated substring
  • Finding longest common substring of two strings

Building suffix trees efficiently (Ukkonen's algorithm) is complex. In competitive programming, simpler approaches often suffice.