How Kasai builds the LCP array from a suffix array

Short answer: Kasai’s algorithm builds the LCP array of a string from its suffix array in O(n) time. LCP[i] is the length of the longest common prefix of the suffixes that are adjacent in sorted order, SA[i−1] and SA[i]. The algorithm walks the suffixes in text order and uses the inequality LCP[rank[i]] ≥ LCP[rank[i−1]] − 1, so the comparison window only moves forward. The total number of character comparisons is linear.


What the problem actually asks

The suffix array sorts suffixes and does not say how much neighboring suffixes share. Almost every interesting query, longest repeated substring, distinct substring counts, pattern search with fewer comparisons, needs that shared length. Computing each LCP by a fresh scan is O(n²) on a repetitive string.

Kasai’s inequality says the suffix that starts one position after a suffix with a long LCP against its sorted neighbor still has a long LCP against its own sorted neighbor, off by at most one. The window of verified characters therefore slides.


What the algorithm guarantees

Given SA and its inverse rank, the algorithm fills LCP in O(n) character comparisons and O(n) extra memory. LCP[i] for i from 1 to n−1 is the common-prefix length of SA[i−1] and SA[i]. LCP[0] is unused or zero. The longest value in the array is the length of the longest repeated substring, achieved by some pair of adjacent suffixes in sorted order, because a non-adjacent pair’s common prefix cannot exceed the minimum LCP between them.

The inequality is a lower bound the algorithm uses to skip comparisons. It is not a license to skip the verification of the remaining characters. A wrong inverse rank, off by one, compares unrelated suffixes and the values are garbage while still looking like an array of integers.


The working steps

  1. Build the inverse rank so rank[SA[i]] = i. The suffix that starts at position p sits at sorted index rank[p].
  2. Set k = 0. This k is the length already known to match for the current text position.
  3. For text position i from 0 to n−1, if rank[i] is 0, set k = 0 and continue. Otherwise let j = SA[rank[i]−1], the suffix just above i in sorted order.
  4. While both suffixes still have characters and s[i+k] equals s[j+k], increment k. Store LCP[rank[i]] = k.
  5. If k > 0, decrement k once before the next i. The next text position inherits k−1 matched characters.

The linear bound needs the already built suffix array and its inverse. It does not build the suffix array. Prefix doubling or SA-IS does that. Range-minimum queries on the LCP array answer the longest common prefix of any two suffixes, not only adjacent ones, and that structure is a sparse table or a segment tree on top of Kasai’s output.


Approaches that look simpler and fail

Approach Why it fails when the inherited length is not reduced by one
Carry k unchanged to the next text position The next suffix dropped the first character. Keeping k compares one character too far and can store an LCP that is longer than the truth.
Reset k to 0 at every position The array comes out correct and the comparisons are quadratic on a string of identical characters. The decrement is the linear bound.
Compare SA[i] with SA[i+1] while iterating in text order without the inverse Adjacent positions in the text are not adjacent in the suffix array. The neighbor has to be looked up through rank.
Take the maximum LCP over all pairs by scanning a Cartesian product The maximum adjacent LCP is already the global maximum common prefix of any two distinct suffixes.

The suffix array itself is prefix doubling or a linear construction. A constant-time longest common extension of two arbitrary suffixes is a range-minimum query on this LCP array. Kasai’s pass is the linear bridge between them.


A working example

This Python fragment builds the LCP array from a suffix array. The LCP of the suffix at rank r is at least the previous LCP minus one, so each text index is charged a constant amount of work. It prints the first three LCP entries 0 1 3.

text = "banana"
sa = sorted(range(len(text)), key=lambda i: text[i:])
rank = [0] * len(text)
for r, i in enumerate(sa):
    rank[i] = r
lcp = [0] * len(text)
k = 0
for i in range(len(text)):
    if rank[i] == 0:
        k = 0
        continue
    j = sa[rank[i] - 1]
    while i + k < len(text) and j + k < len(text) and text[i + k] == text[j + k]:
        k += 1
    lcp[rank[i]] = k
    if k:
        k -= 1
naive = [0] * len(text)
for r in range(1, len(text)):
    a, b = sa[r], sa[r - 1]
    while a < len(text) and b < len(text) and text[a] == text[b]:
        naive[r] += 1
        a += 1
        b += 1
assert lcp == naive
print(*lcp[:3])

Where it is used

Kasai’s algorithm builds the LCP array from a finished suffix array, which is the neighbor-height table in genome indexes and in string search. The suffix array alone does not give those heights. An FM-index then uses the Burrows-Wheeler transform of the same string.


When you can drop this algorithm

Drop a hand-written LCP loop that resets to zero when the suffix array is large and repetitive. The inherited length is the whole point. Keep Kasai’s algorithm whenever an LCP array is required next to a suffix array.


Why this stays published

We publish this so a search for Kasai’s algorithm returns the inequality that lets the matched length drop by only one as the text position advances. Kasai’s algorithm builds the LCP array in linear time by walking suffixes in text order and reusing the previous matched length minus one.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *