3.8.7 · D1String Algorithms

Foundations — Suffix array — construction O(n log n), LCP array

1,890 words9 min readBack to topic

This page builds every symbol the parent note uses, starting from "what is a string" and never using a piece of notation before it is drawn and explained. If a symbol below already feels obvious, skim it — but each later idea leans on the earlier ones, so the order matters.


1. A string and its characters

Picture a row of numbered boxes. Box number 0 holds the first letter.

Figure — Suffix array — construction O(n log n), LCP array
  • Why the topic needs it: the suffix array is a list of positions, so "position" must be rock solid.

2. A suffix — a "tail" of the string

If then (the whole thing), , and (just the last letter). There are exactly suffixes — one starting at each position.

Figure — Suffix array — construction O(n log n), LCP array
  • Why the topic needs it: the suffix array sorts these objects.

3. Prefix and "longest common prefix"

Line the two strings up left-aligned and read across until the first mismatch. The number of matching columns is the LCP length.

  • Why the topic needs it: the entire LCP array (Section 3 of the parent) is just this number computed for neighboring sorted suffixes.

4. Lexicographic order — "dictionary order"

We reuse the ordinary alphabet order for single characters: . In "banana" the only letters are with .

Figure — Suffix array — construction O(n log n), LCP array
  • Why the topic needs it: "sorted lexicographically" is the definition of the suffix array.

5. The suffix array and the meaning of

Read it as: "go to shelf number in the sorted bookshelf; which position did that tail start at?"

Figure — Suffix array — construction O(n log n), LCP array
  • Why the topic needs it: is the output of construction. Everything downstream (binary search, LCP) reads it.

6. The rank array — the inverse of

If then position sits on shelf , position on shelf , and so on, giving (read: position is on shelf ).

  • Why the topic needs it: the predecessor lookup literally chains both arrays.

7. The subscript and the doubling ranks

Start with (sort by first letter alone), then , , , doubling each round until , at which point "first chars" already covers the whole suffix.

Figure — Suffix array — construction O(n log n), LCP array
  • Why the topic needs it: this is the engine. The is the number of doublings.

8. Big-O notation and

  • Why the topic needs it: the number of doubling rounds is exactly , so the cost label names "linear work per doubling round × number of rounds."

9. The letter — the running LCP length in Kasai

  • Why the topic needs it: is why LCP construction is instead of ; without carrying you re-scan everything.

Prerequisite map

Characters s at i (0-indexed)

Suffix suf of i

Prefix and LCP length

Character order a lt b lt n

Lexicographic order of strings

Suffix array SA of r

Rank array inverse of SA

Doubling ranks r sub k

Construction O n log n

Kasai LCP with carried h

Parent topic


Equipment checklist

Self-test: can you answer each before revealing?

What does mean and where does counting start?
The character at position , counting from , so is the first and the last.
What is ?
The tail of from position to the end, ; there are of them.
Why does every substring relate to suffixes?
Every substring is a prefix (front chunk) of some suffix, so studying suffixes covers all substrings.
How do you compare two strings lexicographically?
Compare character by character from the left; the first mismatch decides; if one runs out while matching, the shorter one is smaller.
What is in words?
The starting index of the -th smallest suffix in dictionary order.
How are and related?
They are inverse permutations: ; maps shelf→position, maps position→shelf.
What is and why doubling?
Rank of suffix using only its first chars; doubling lets the first chars be compared as the pair using already-known ranks.
Why is used when ?
A suffix that ran off the end acts like an empty/smallest character, so it must sort before any real character — encoded as .
What does mean here?
About doubling rounds, each doing linear () work.
What does do in Kasai's algorithm?
Carries the current overlap length across positions, shrinking by at most 1 per step, giving linear-time LCP.