Skip to content
Advanced

Suffix Arrays

A sorted array of all suffixes of a string.

Time Complexity
O(N log N)
Space Complexity
O(N)

Interactive Visualization

Original String: BANANA
[0] BANANA
[1] ANANA
[2] NANA
[3] ANA
[4] NA
[5] A
Sorted Suffix Array
A
ANA
ANANA
BANANA
NA
NANA

Technical Guide

A Suffix Array is a sorted array of all suffixes of a string. It is a powerful space-efficient alternative to Suffix Trees. Once built, we can find any pattern of length M in the text using Binary Search in O(M log N) time. Advanced construction algorithms like SA-IS can build the array in linear O(N) time.

Logical Phases

  • 1
    Generating all N suffixes of the string.
  • 2
    Sorting the suffixes lexicographically.
  • 3
    Storing only the starting indices of the sorted suffixes.
  • 4
    Using the array for fast substring queries.

Implementation

suffixes.sort(); // Simplest O(N^2 log N) way

Expert Pitfalls

Building the array by actually copying and sorting strings is O(N^2 log N); professional versions use pointers.