Z-Algorithm — Linear Prefix Matching for All Positions
The Z-algorithm computes Z[i] = length of the longest substring starting at i that matches a prefix of the string. O(n) time, elegant and powerful for pattern search.
1575 articles
The Z-algorithm computes Z[i] = length of the longest substring starting at i that matches a prefix of the string. O(n) time, elegant and powerful for pattern search.
Rabin-Karp uses a rolling polynomial hash to match patterns in O(n+m) expected time. The key is updating the hash in O(1) as the window slides.
Find the longest palindromic substring in O(n) using Manacher's algorithm. Expands palindromes using a mirror trick to avoid redundant work.
Precompute polynomial prefix hashes to compare any two substrings in O(1). Enables O(n log n) LCP computation and O(n) duplicate detection.
Build a suffix array in O(n log²n) using doubling sort. Enables O(m log n) substring search, longest common substring, and number of distinct substrings.