Z-Algorithm
The Z-algorithm produces the ”-array” in time, where each element of the array indicates the length of the longest substring of that begins at position and is a prefix of .
Resources
For example, here are the values of the Z-function computed for different strings:
- “aaaaa” -
- “aaabaab” -
- “abacaba” -
Why is it 0 for the first index?
Z[0] is 0 here by convention. From cp-algorithms website:
- “The first element of Z-function, , is generally not well defined. In this article we will assume it is zero (although it doesn’t change anything in the algorithm implementation).”
Many string processing problems can be efficiently solved using the Z-array.
Problems that you can solve with the Z-Algorithm
Search the substring: find all occurrences of the pattern inside the text . Solution: Construct a new string , we use as a separator. Then, go over the z-array and count the number of indices where