Advanced Topic 37 of 52 Data Structures
Suffix Arrays
Sorted array of all suffixes of a string.
How it works
A suffix array lists the starting indexes of all suffixes of a string in sorted order. It takes far less memory than a suffix tree, needs only n integers and supports substring search with binary search in O(m log n). Combined with an LCP array of longest common prefixes between neighbors, it answers many questions about repeated substrings. Construction can be done in O(n log n) or even O(n).
Step-by-step walkthrough
- Take all suffixes of the string banana.
- Sort them alphabetically: a, ana, anana, banana, na, nana.
- Store only their start positions: 5 3 1 0 4 2.
- Binary search finds any pattern in O(m log n).
- Neighbors in the sorted order share long prefixes.
Where it is used
Full-text search, genome alignment, data compression via the Burrows-Wheeler transform, finding the longest repeated or common substring and plagiarism detection use suffix arrays.
Common pitfall
Naive construction by sorting suffix strings costs O(n squared log n) in the worst case. Handling a sentinel character and comparing equal prefixes is error-prone. For multiple strings, concatenation with unique separators is needed. Remember the array holds positions, not substrings.
Where it fits in the course
Suffix Arrays is topic 37 of 52 in Data Structures, rated Advanced. It follows Sparse Tables. The next topic is Suffix Trees.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Data Structures quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Data Structures quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Suffix Arrays?
Where do people use Suffix Arrays?
What is the most common mistake with Suffix Arrays?
What should I learn before this topic?
How can I practice it?
Is Suffix Arrays hard to learn?
How long does it take to learn Suffix Arrays?
Which topics are related to Suffix Arrays?
What should I study after Suffix Arrays?
Related topics
- Sparse Tables (Data Structures)
- Suffix Trees (Data Structures)
- Treaps (Data Structures)
- Interval Trees (Data Structures)
- Arrays and Lists (Programming Fundamentals)