Skip to main content
๐Ÿ“œ WAYPOINT LESSON

Suffix Arrays and LCP

โญโญโญ advancedโณ 30 min read๐Ÿ“ Lesson 167 of 281

Sort suffixes by doubling rank pairs, then Kasai's walk gives every adjacent LCP in O(n) โ€” distinct substrings = n(n+1)/2 minus the LCP sum.

Order, then count

Suffix-array construction by rank doubling is O(n log^2 n) with a plain sort โ€” fast enough for n = 2000 course loads. Kasai's algorithm walks the text left-to-right, each step's LCP dropping at most one: O(n) total. The counting identity n(n+1)/2 - ฮฃ LCP falls out of how sorted suffixes share prefixes.

โšก Now practice

Ready to Code
String TrioHash-based per-length distinct counting, Z-array construction at 100k, and Z-based pattern matching with overlaps.
3 challenges ยท ยท ~110 min