Bölüm 5 · Simülatör

KMP Prefix Function & Z Algorithm

← Bölüm 5

Prefix function π and Z array (click a column)

Ready.

KMP Prefix Function & Z Algorithm

Compare two classic linear-time string preprocessing arrays on the same input.

How to use

  • Type a string (at most 40 characters) or pick an example, then press Build Arrays (Enter also works).
  • The table lists, for every index i, the character s[i], the prefix function value π[i] and the Z value Z[i]. Strings longer than 20 characters are split into two blocks.
  • Click a column to see what its two values mean: the strips below highlight the prefix and the block of the string that match.

Prefix function (π)

π[i] is the length of the longest proper prefix of s[0..i] that is also a suffix. KMP uses it to continue matching after a mismatch without moving back in the text.

Z array

Z[i] is the longest prefix length matching the substring starting at i. By convention Z[0] is shown as 0. To search for a pattern P in a text T, compute Z on P + "$" + T: every position with Z[i] = |P| is a match.

Complexity

ArrayTimeMemory
Prefix function πO(n)O(n)
Z arrayO(n)O(n)