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.
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.
| Algorithm | Why it matters |
|---|---|
| Knuth–Morris–Pratt | Uses a failure table so it never steps back in the text. O(n + m), and the classic string algorithm in every textbook. |
| Rabin–Karp | Compares rolling hashes instead of characters. O(n + m) on average, and a good lesson in spurious hits. |
| Boyer–Moore–Horspool | Compares from the end of the pattern and skips ahead with a bad-character table. Sublinear on average. |
| Z-Algorithm | Builds the Z-array of longest prefix matches in O(n + m), with a moving Z-box that reuses earlier work. |
| Finite Automata Matching | Builds a DFA for the pattern and scans the text in O(n). KMP is a compressed form of this automaton. |
| Aho–Corasick | Finds 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.
| Algorithm | Why it matters |
|---|---|
| Suffix Array | The 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 Substring | The 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.
| Algorithm | Why it matters |
|---|---|
| Manacher’s Algorithm | Finds the longest palindromic substring in O(n), compared with O(n²) for expanding around each centre. |
| Booth’s Algorithm | Finds 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.
| Algorithm | Why it matters |
|---|---|
| Longest Substring Without Repeats | The standard sliding-window problem, with a hash map of last-seen positions. |
| Longest Common Substring | A clear example of a two-dimensional dynamic programming table. |
| Word Ladder | The shortest chain of one-letter changes between two words, using BFS on an implicit graph. |
| Permutations of a String | Backtracking by swapping and fixing one position at a time. |
| All Subsequences | Recursion that generates all 2ⁿ subsequences by including or excluding each character. |
| Letter Combinations of a Phone Number | Backtracking over the letters of a phone keypad, building the combinations one digit at a time. |
