String Algorithms — Master Recap and Pattern Cheatsheet
Advertisement
String Algorithms Master Recap
Complete cheatsheet for all 20 string algorithm problems in this series, with algorithm selection guide and complexity reference.
Problem Index
| # | Problem | Algorithm | Complexity |
|---|---|---|---|
| 00 | Complete Guide | 6 patterns + complexity table | — |
| 01 | KMP Search | Failure function | O(n+m) |
| 02 | Z-Algorithm | Z-array | O(n+m) |
| 03 | Rabin-Karp | Rolling hash | O(n+m) avg |
| 04 | Manacher | Palindrome expansion | O(n) |
| 05 | String Hashing | Polynomial hash | O(1) query |
| 06 | Suffix Array | DC3 / SA-IS | O(n log n) |
| 07 | Longest Common Substring | DP | O(nm) |
| 08 | String DP | Edit distance, LCS | O(nm) |
| 09 | Anagram Problems | Frequency vector | O(n) |
| 10 | Palindrome Suite | Expand, DP, min-cut | Various |
| 11 | Word Search I/II | DFS + Trie | O(MN4^L) |
| 12 | Encode/Decode | Length-prefix | O(n) |
| 13 | Shortest Palindrome | KMP on s+'#'+rev | O(n) |
| 14 | Distinct Substrings | Suffix array + LCP | O(n log n) |
| 15 | String Compression | Run-length | O(n) |
| 16 | Trie Advanced | XOR trie, palindrome pairs | O(n*L) |
| 17 | Meta/Google Strings | No-repeat window, valid parens | O(n) |
| 18 | Aho-Corasick | Multi-pattern automaton | O(n+m+z) |
| 19 | Master Recap | This file | — |
Algorithm Selection Guide
Single pattern search in text?
→ KMP O(n+m) — exact match, worst-case guaranteed
→ Rabin-Karp O(n+m) expected — multiple length patterns
All prefix occurrences / repeated substring?
→ Z-algorithm O(n+m)
→ KMP failure function at last position
Longest palindromic substring?
→ Manacher O(n) — fastest
→ Expand around center O(n^2) — simpler to implement
O(1) substring comparison?
→ Polynomial string hashing with prefix array
Lexicographic substring queries (LCP, distinct count)?
→ Suffix array O(n log n) build + O(1) LCP
Multi-pattern search?
→ Aho-Corasick O(n + total_pattern_length + z)
Edit distance / LCS?
→ Classic DP O(nm), optimize with rolling array to O(n)
Count palindromic substrings?
→ Expand O(n^2) or Manacher O(n)Complexity Summary
| Algorithm | Time | Space | Best For |
|---|---|---|---|
| KMP | O(n+m) | O(m) | Single pattern, worst-case guarantee |
| Z-function | O(n+m) | O(n+m) | Prefix occurrences |
| Rabin-Karp | O(n+m) avg | O(1) | Multiple patterns, expected bound |
| Manacher | O(n) | O(n) | All palindromes in one pass |
| String hash | O(n) build, O(1) query | O(n) | Substring equality in O(1) |
| Suffix array | O(n log^2 n) | O(n) | LCP, distinct substrings |
| Edit distance | O(nm) | O(n) rolling | Similarity, alignment |
| Aho-Corasick | O(n+m+z) | O(m * ALPHA) | Multi-pattern matching |
KMP Failure Function Template
def build_lps(pattern):
m = len(pattern)
lps = [0] * m
length = 0
i = 1
while i < m:
if pattern[i] == pattern[length]:
length += 1
lps[i] = length
i += 1
elif length > 0:
length = lps[length - 1] # do NOT increment i here
else:
lps[i] = 0
i += 1
return lpsRolling Hash Template
MOD = (1 << 61) - 1 # Mersenne prime
BASE = 131
def build_hash(s):
n = len(s)
h = [0] * (n + 1)
p = [1] * (n + 1)
for i in range(n):
h[i+1] = (h[i] * BASE + ord(s[i])) % MOD
p[i+1] = p[i] * BASE % MOD
def get(l, r): # hash of s[l..r] inclusive, 0-indexed
return (h[r+1] - h[l] * p[r-l+1]) % MOD
return getZ-Algorithm Template
def z_function(s):
n = len(s)
z = [0] * n
z[0] = n
l = r = 0
for i in range(1, n):
if i < r:
z[i] = min(r - i, z[i - l])
while i + z[i] < n and s[z[i]] == s[i + z[i]]:
z[i] += 1
if i + z[i] > r:
l, r = i, i + z[i]
return zCommon Pitfalls
- KMP: forgetting
j = lps[j-1]on mismatch. The whole point is NOT resetting j to 0. - KMP on combined string for Shortest Palindrome: forgetting the
#separator. Without it, the LPS bleeds across the boundary. - Rolling hash: not using a Mersenne prime modulus. Small primes collide frequently on adversarial inputs.
- Manacher: mixing 0-indexed and 1-indexed. Insert
#separators consistently and never mix conventions. - Aho-Corasick: forgetting to propagate output links. Patterns that are suffixes of other patterns will be missed.
- Suffix array: using naive O(n^2 log n) sort. Build with the O(n log n) doubling approach for large n.
Interview Mindset
- For single pattern search, KMP is the expected O(n+m) answer. Always explain the failure function, not just the mechanics.
- For palindromes, offer Manacher as the O(n) solution but acknowledge that expanding from center is simpler if Manacher implementation is not required.
- For hashing problems, mention the double-hash technique (two bases/moduli) to reduce collision probability.
- When a problem involves many string queries on the same text, think suffix array or hashing — not repeated KMP.
Key Takeaways
- KMP achieves O(n+m) by never moving the text pointer backward — the failure function encodes the pattern's self-similarity.
- The Z-array
z[i]= length of longest substring starting froms[i]that matches a prefix ofs— directly enables pattern search by concatenatingpattern + '#' + text. - Manacher's algorithm computes all palindromic substrings in O(n) by exploiting previously computed palindrome radii to skip redundant checks.
- Rolling hash enables O(1) substring comparison after O(n) preprocessing — use a Mersenne prime modulus to minimize collisions.
- Aho-Corasick extends KMP failure links to a trie, enabling simultaneous O(n + m + z) search for k patterns in a single text pass.
- Suffix arrays solve the hardest substring problems (LCP, distinct substring count, lexicographic ordering) in O(n log n) space and time.
- The right algorithm depends on the query type: single pattern → KMP; multi-pattern → Aho-Corasick; palindromes → Manacher; substring equality → hashing.
Advertisement
Related reading
Math and Number Theory — Master Recap and Interview Cheatsheet6 min readTries — Master Recap and Interview Cheatsheet5 min readArrays & Strings Complete — 100-Problem Master Cheatsheet6 min readMajority Element — Boyer-Moore Voting Algorithm Explained Deeply [LeetCode 169]18 min readGroup Anagrams — Hashmap Key Design Mastery [Amazon, Google, Meta]13 min readLongest Substring Without Repeating Characters — Sliding Window [Google, Amazon, Meta]14 min read