DSAExpertguide

Suffix Arrays and Suffix Trees in Java: Advanced String Structures

Master Suffix Arrays and Suffix Trees in Java. Learn how these advanced structures enable O(m log n) substring searching and solve complex string problems in linear time.

Sachin SarawgiApril 19, 20263 min read3 minute lesson
Recommended Prerequisites
Z-Algorithm in Java: Linear Time String Matching

When it comes to advanced string processing, Suffix Trees and Suffix Arrays are the heavy hitters. They allow you to answer complex queries about a string—like "What is the longest repeated substring?" or "How many times does pattern P appear?"—extremely fast.

While Suffix Trees are powerful, Suffix Arrays are often preferred in interviews and competitive programming because they are much more memory-efficient and easier to implement.

1. What is a Suffix Array?

A Suffix Array is simply a sorted array of all suffixes of a given string. Instead of storing the actual strings (which would be $O(n^2)$ space), we store the starting indices of the suffixes.

Example for "banana$":

  1. Suffixes: banana$, anana$, nana$, ana$, na$, a$, $
  2. Sorted Suffixes: $, a$, ana$, anana$, banana$, na$, nana$
  3. Suffix Array: [6, 5, 3, 1, 0, 4, 2]

2. Suffix Array Implementation in Java (Simple $O(n^2 \log n)$)

A truly efficient Suffix Array construction takes $O(n \log n)$ or even $O(n)$, but the "simple" version is often enough to demonstrate the concept.

import java.util.*;

public class SuffixArray {
    static class Suffix implements Comparable<Suffix> {
        int index;
        String text;

        Suffix(int index, String text) {
            this.index = index;
            this.text = text;
        }

        @Override
        public int compareTo(Suffix other) {
            return this.text.compareTo(other.text);
        }
    }

    public int[] buildSuffixArray(String s) {
        int n = s.length();
        Suffix[] suffixes = new Suffix[n];

        for (int i = 0; i < n; i++) {
            suffixes[i] = new Suffix(i, s.substring(i));
        }

        Arrays.sort(suffixes);

        int[] sa = new int[n];
        for (int i = 0; i < n; i++) {
            sa[i] = suffixes[i].index;
        }
        return sa;
    }
}

3. What is a Suffix Tree?

A Suffix Tree is a compressed Trie of all suffixes of a string. Every path from the root to a leaf represents a suffix.

Why use a Suffix Tree?

  • Substring search: $O(m)$ time (where $m$ is pattern length).
  • Find longest repeated substring: $O(n)$ time.
  • Find longest common substring of two strings: $O(n + m)$ time.

Suffix Array vs. Suffix Tree

Feature Suffix Tree Suffix Array
Space Complexity $O(n)$ but high constant $O(n)$ (very compact)
Construction Time $O(n)$ (Ukkonen's Algorithm) $O(n \log n)$ or $O(n)$
Search Time $O(m)$ $O(m \log n)$ (Binary Search)
Complexity Hard to implement Easier to implement

Summary

Suffix structures are the peak of string algorithmic design. While building a Suffix Tree from scratch is rarely required in a standard interview, understanding how a Suffix Array combined with an LCP (Longest Common Prefix) Array can solve almost any string problem is a hallmark of a senior-level algorithm engineer.

📚

Recommended Resources

Java Masterclass — UdemyBest Seller

Comprehensive Java course covering Java 17+, OOP, concurrency, and modern APIs.

View Course
Effective Java, 3rd EditionMust Read

Joshua Bloch's classic guide to writing clear, correct, and efficient Java code.

View on Amazon
Java Concurrency in Practice

The authoritative book on writing thread-safe, concurrent Java programs.

View on Amazon

Practical engineering notes

Get the next backend guide in your inbox

One useful note when a new deep dive is published: system design tradeoffs, Java production lessons, Kafka debugging, database patterns, and AI infrastructure.

No spam. Just practical notes you can use at work.

Sachin Sarawgi

Written by

Sachin Sarawgi

Engineering Manager and backend engineer with 10+ years building distributed systems across fintech, enterprise SaaS, and startups. CodeSprintPro is where I write practical guides on system design, Java, Kafka, databases, AI infrastructure, and production reliability.

Keep Learning

Move through the archive without losing the thread.

Related Articles

More deep dives chosen from shared tags, category overlap, and reading difficulty.

More in DSA

Category-based suggestions if you want to stay in the same domain.