Data Structures · Visualization

Suffix Array: LCP Array

Suffix Array

The LCP (Longest Common Prefix) array stores, for each pair of neighboring suffixes in the sorted suffix array, how many leading characters they share. Time: O(n) with Kasai's algorithm (after the suffix array is built), O(n^2) naive. Space: O(n).

Big-O: Build O(n log n); pattern search O(m log n).

Suffix Array: LCP Array

Suffix Array

The LCP (Longest Common Prefix) array stores, for each pair of neighboring suffixes in the sorted suffix array, how many leading characters they share. Since alphabetically similar suffixes sit next to each other, this reveals repeated patterns in the original string very efficiently, and is a key ingredient for many advanced string algorithms.

How LCP Array Works

  1. Start with the suffix array already built and sorted.
  2. For each pair of adjacent suffixes in the sorted order, compare them character by character from the start.
  3. Count how many characters match before the first difference (or until one suffix ends).
  4. Store that count as the LCP value for that adjacent pair.

LCP Array Worked Example

Input: Suffix array [5,3,1,0,4,2] for 'banana' -> suffixes a, ana, anana, banana, na, nana

  1. LCP('a', 'ana') = 1 (share 'a')
  2. LCP('ana', 'anana') = 3 (share 'ana')
  3. LCP('anana', 'banana') = 0 (share nothing)
  4. LCP('banana', 'na') = 0
  5. LCP('na', 'nana') = 2 (share 'na')

Output: [1, 3, 0, 0, 2]

Try your own input
Suffix Array: Introduction
A sorted list of every suffix (ending substring) of a string, stored as starting indices — it makes substring search very fast.
Press ▶ Run to build the suffix array.
Suffix Array: Introduction
Press ▶ Run to watch it step by step.
Python · Suffix Arrays in Code
def kasai(s, sa):
    n = len(s)
    rank = [0] * n                 # inverse permutation of sa
    for i in range(n): rank[sa[i]] = i
    lcp = [0] * (n - 1)
    h = 0
    for i in range(n):             # O(n)
        r = rank[i]
        if r == n - 1: h = 0; continue
        j = sa[r + 1]
        while i + h < n and j + h < n and s[i + h] == s[j + h]:
            h += 1
        lcp[r] = h
        if h > 0: h -= 1
    return lcp                     # lcp[r] = LCP(sa[r], sa[r+1])
# example: lcp array
Input: Suffix array [5,3,1,0,4,2] for 'banana' -> suffixes a, ana, anana, banana, na, nana

Behind the Scenes Execution

Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.

LCP Array Time & Space Complexity

TimeO(n) with Kasai's algorithm (after the suffix array is built), O(n^2) naive
SpaceO(n)

When to Use LCP Array

  • Finding the longest repeated substring in a text
  • Efficient string comparison and pattern-matching algorithms
  • Data compression techniques that rely on repeated substrings

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.