WikiGalaxy

Personalize

Suffix Array: Introduction

What is a Suffix Array?

A suffix array is a data structure that provides a sorted array of all suffixes of a given string. It is used in various applications such as pattern matching, data compression, and bioinformatics.

Why Use Suffix Arrays?

Suffix arrays offer a space-efficient alternative to suffix trees while maintaining the ability to perform efficient pattern matching and other operations.

Example 1: Basic Suffix Array Construction

Constructing a Suffix Array

Given the string "banana", the suffixes are ["banana", "anana", "nana", "ana", "na", "a"]. The sorted suffixes are ["a", "ana", "anana", "banana", "na", "nana"]. The suffix array is [5, 3, 1, 0, 4, 2].


String text = "banana";
int[] suffixArray = buildSuffixArray(text);
System.out.println(Arrays.toString(suffixArray)); // Output: [5, 3, 1, 0, 4, 2]
    

Example 2: Pattern Matching with Suffix Arrays

Using Suffix Arrays for Pattern Matching

To find if a pattern exists in a text, use binary search on the suffix array. For example, to find "ana" in "banana", check the sorted suffixes using the suffix array.


boolean patternExists = patternMatching("banana", "ana", suffixArray);
System.out.println(patternExists); // Output: true
    

Example 3: Longest Repeated Substring

Finding the Longest Repeated Substring

Using the suffix array and the longest common prefix (LCP) array, identify the longest repeated substring in a string.


String longestRepeatedSubstring = findLongestRepeatedSubstring("banana");
System.out.println(longestRepeatedSubstring); // Output: "ana"
    

Example 4: Suffix Array for Lexicographical Order

Sorting Substrings Lexicographically

Use the suffix array to sort all substrings of a string lexicographically. This is useful in problems involving lexicographical order.


List sortedSubstrings = getSortedSubstrings("banana");
System.out.println(sortedSubstrings); // Output: ["a", "ana", "anana", "banana", "na", "nana"]
    

Example 5: Space Optimization with Suffix Arrays

Efficient Space Usage

Suffix arrays use O(n) space, which is more efficient than the O(n log n) space used by suffix trees. This makes them suitable for large-scale data processing.


int[] optimizedSuffixArray = buildOptimizedSuffixArray("large_text_input");
System.out.println(Arrays.toString(optimizedSuffixArray));
    
logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025