String Algorithms
Search, compare and analyze strings in linear or near-linear time with the Z-function, polynomial hashing, Manacher's algorithm and a multi-pattern automaton.
4 objectives 5 steps about 120 min
Check what you know
The lessons are coming later. The knowledge check is ready now.
This skill page is a public outline. Its lessons and knowledge check appear here as subject-matter experts approve them.
Learning objectives
- Compute the Z-array of a string in O(n) and use it to find every occurrence of a pattern and the shortest period of a string.
- Compare any two substrings in O(1) after O(n) preprocessing with polynomial prefix hashes, choose the base and modulus to keep collisions unlikely, and combine hashing with binary search on length to find a longest repeated substring.
- Find the longest palindromic substring by expanding around centers in O(n^2) and with Manacher's algorithm in O(n), and explain how reusing a mirrored radius avoids rescanning.
- Build an Aho-Corasick automaton over a trie of patterns and report every occurrence of every pattern in one pass over the text.
Lesson plan
5 steps, about 120 min · 0 of 4 lessons available so far
- The Z-Array: Matching Against Your Own Prefix Worked example · 25 min · coming later
- Prefix Hashes for Constant-Time Substring Checks Worked example · 25 min · coming later
- Palindromes from the Center Out, Then Manacher Worked example · 25 min · coming later
- Lab: Scanning Logs for Many Patterns at Once Lab · 30 min · coming later
- Check: String Algorithm Problems Knowledge check · 8 questions
Practise it
Implement the Z-function, a prefix-hash class with substring equality, Manacher's algorithm and an Aho-Corasick matcher; cross-check each against brute force on 2,000 Hypothesis-generated strings over a two-letter alphabet, where overlaps and repeats are common; then scan a 50 MB log for a 1,000-term blocklist and compare the runtime with running KMP once per term.
References
Hard prerequisites
Unlocks
Nothing depends on this skill yet.
On these roadmaps
Progress and evidence for this skill are shared across all of them.