Post

String Algorithm Reference

A reference list of the string algorithms in the Algorithm Guide, grouped into pattern matching, suffix structures, palindromes and rotations, and string problems, with a short note on why each one matters.

String Algorithm Reference

Every string algorithm covered by the Algorithm Guide, grouped into four sections: finding a pattern in a text, the suffix structures behind fast substring queries, palindromes and rotations, and the classic string problems that teach a general technique. Algorithms with a guide link to it; the rest are on the way.

Pattern Matching

Finding one or more patterns inside a text, also known as string searching. Each algorithm avoids some of the work a naive search repeats.

AlgorithmWhy it matters
Knuth–Morris–PrattUses a failure table so it never steps back in the text. O(n + m), and the classic string algorithm in every textbook.
Rabin–KarpCompares rolling hashes instead of characters. O(n + m) on average, and a good lesson in spurious hits.
Boyer–Moore–HorspoolCompares from the end of the pattern and skips ahead with a bad-character table. Sublinear on average.
Z-AlgorithmBuilds the Z-array of longest prefix matches in O(n + m), with a moving Z-box that reuses earlier work.
Finite Automata MatchingBuilds a DFA for the pattern and scans the text in O(n). KMP is a compressed form of this automaton.
Aho–CorasickFinds many patterns in one pass using a trie with failure links. Used in keyword filters and antivirus scanners.

Suffix Structures

A three-part series where each structure builds on the one before.

AlgorithmWhy it matters
Suffix ArrayThe sorted list of all suffixes of a string, and the basis for fast substring queries.
Kasai’s Algorithm (LCP)Builds the LCP (longest common prefix) array from a suffix array in O(n).
Longest Repeated SubstringThe longest substring that occurs at least twice, read straight off the suffix array and the LCP array.

Palindromes & Rotations

Linear-time algorithms on palindromes and cyclic strings that are elegant but rarely explained well in Java.

AlgorithmWhy it matters
Manacher’s AlgorithmFinds the longest palindromic substring in O(n), compared with O(n²) for expanding around each centre.
Booth’s AlgorithmFinds the lexicographically smallest rotation in O(n), which puts cyclic strings in a canonical form.
Lyndon Factorization (Duval)Splits a string into a non-increasing sequence of Lyndon words in O(n).

String Problems

String problems that are best known for the general technique they teach: sliding windows, dynamic programming, BFS and backtracking.

AlgorithmWhy it matters
Longest Substring Without RepeatsThe standard sliding-window problem, with a hash map of last-seen positions.
Longest Common SubstringA clear example of a two-dimensional dynamic programming table.
Word LadderThe shortest chain of one-letter changes between two words, using BFS on an implicit graph.
Permutations of a StringBacktracking by swapping and fixing one position at a time.
All SubsequencesRecursion that generates all 2ⁿ subsequences by including or excluding each character.
Letter Combinations of a Phone NumberBacktracking over the letters of a phone keypad, building the combinations one digit at a time.
This post is licensed under CC BY 4.0 by the author.