Published by Dimitris Kougioumtzis · Updated
Reuse the previous common-prefix length; LCP[0] is zero. The suffix array is prepared before the trace.
Use inverse suffix ranks and reuse the previous common-prefix length minus one to avoid repeated character scanning.
When to use Kasai longest common prefix array
Measuring shared prefixes and repeated text regions.
Choosing an alternative
Suffix arrays provide order; Kasai adds adjacent common-prefix lengths.
Worked example: input and state changes
Example input
banana
These checkpoints follow the actual example timeline. State details show the stored values and decisions; use the visualization to inspect every intervening step.
Step 0: Initial state.
State at this checkpoint
{ "values": [ 0, 0, 0, 0, 0, 0 ], "active": [] }Step 2: Extend common prefix of suffixes 1 and 3 to 1.
State at this checkpoint
{ "values": [ 0, 0, 0, 0, 0, 0 ], "active": [ 2 ], "suffixArray": [ 5, 3, 1, 0, 4, 2 ] }Step 5: LCP[2] = 3.
State at this checkpoint
{ "values": [ 0, 0, 3, 0, 0, 0 ], "active": [ 2 ], "suffixArray": [ 5, 3, 1, 0, 4, 2 ] }Step 7: LCP[1] = 1.
State at this checkpoint
{ "values": [ 0, 1, 3, 0, 0, 2 ], "active": [ 1 ], "suffixArray": [ 5, 3, 1, 0, 4, 2 ] }Step 9: Longest common prefix array complete.
State at this checkpoint
{ "values": [ 0, 1, 3, 0, 0, 2 ], "active": [], "suffixArray": [ 5, 3, 1, 0, 4, 2 ] }
Expected final state
Longest common prefix array complete.
Complexity and input bounds
Family: Strings. Time complexity: O(n) after suffix array construction.
Space complexity: O(n) inverse ranks and LCP output.
Working-space bounds describe algorithm state and any explicitly noted output, excluding the visualization's saved timeline. Recording frames can use more memory than running the function alone. Here n is sequence length, V and E are vertex and edge counts, and other parameters refer to the named input dimensions.
One string of 1–24 ASCII letters.
The playground validates these limits before starting. Downloaded functions are callable implementations, with parameter and output-buffer contracts in their comments; they do not all repeat the playground's input validation.
Common mistakes and limitations
The linear bound assumes an already valid suffix array; building that array is separate work.
Further reading
For background on algorithm analysis and the JavaScript function interface:
- MIT OpenCourseWare: algorithm design and analysis foundations
- MDN: JavaScript function parameters and return values
Editorial and testing approach · Browse all algorithm guides
Complete JavaScript implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 | // 1–24 ASCII letter codes plus a valid sorted suffix array in order. Writes LCP lengths; output[0]=0. Runs in O(n) given the suffix array. Reference snippets assume validated playground inputs. Fixed scratch buffers have 256 slots. Sorting variants may use a different traversal strategy than the animation. function algorithm(a, n, order, output) { let rank = Array(256).fill(0); for (let i = 0; i < n; i++) { rank[order[i]] = i; output[i] = 0; } let h = 0; for (let i = 0; i < n; i++) { let position = rank[i]; if (position == 0) { h = 0; continue; } let j = order[position - 1]; while (i + h < n && j + h < n && a[i + h] == a[j + h]) { h++; } output[position] = h; if (h > 0) { h--; } } return 0; } |
Source
Primary research source for the algorithm or its underlying method. The playground is a bounded teaching implementation, not the publication's original program.
- Linear-Time Longest-Common-Prefix Computation in Suffix Arrays and Its Applications
Toru Kasai; Gunho Lee; Hiroki Arimura; Setsuo Arikawa; Kunsoo Park (2001). Research paper.