Z Function
Reuse a rightmost prefix-matching box to compute every suffix-prefix match length in linear time.
Core idea
For each position, z stores the longest suffix prefix that matches the whole string prefix. The rightmost known match box lets positions inside it reuse a mirrored value.
Read the visualization
The active Z-box marks its left and right boundaries. Inside it, a mirror supplies an initial radius; direct comparisons occur only when that radius reaches the right edge.
Set z at zero to the string length and begin with an empty rightmost Z-box.
Complexity and tradeoffs
Time: O(n). Space: O(n). Comparisons that extend the Z-box move its right boundary forward, so total extension work is linear.
Where it fits
The Z function supports exact pattern matching with pattern-plus-text concatenation, borders, periods, and string compression checks.