Advanced
Suffix Arrays
A sorted array of all suffixes of a string.
Time Complexity
O(N log N)
Space Complexity
O(N)
Interactive Visualization
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
- 1Generating all N suffixes of the string.
- 2Sorting the suffixes lexicographically.
- 3Storing only the starting indices of the sorted suffixes.
- 4Using the array for fast substring queries.
Implementation
suffixes.sort(); // Simplest O(N^2 log N) wayExpert Pitfalls
Building the array by actually copying and sorting strings is O(N^2 log N); professional versions use pointers.