String Cheatsheet¶
Use this page when the string task is close to a known family but you want the fastest route back to the right tool.
Do Not Use When¶
- the bottleneck is still parsing the statement into a string model
- you need the full derivation of KMP / Z / suffix structures
- probabilistic hashing is unacceptable for the task but you are still reaching for it by habit
Choose Fast¶
- one exact pattern in one text: KMP or Z
- one static longest-palindrome scan: Palindromes hot sheet
- one growing string, distinct palindromes, or longest palindromic suffix: Eertree hot sheet
- many substring equality checks: String Hashing hot sheet
- many patterns in one text: Aho-Corasick hot sheet
- one small regex language with
(),|,*,., and full-string membership: Regex / Finite Automata hot sheet - one fixed text used as a compressed substring index: Suffix Tree hot sheet
- suffix-order / repeated-substring / lexicographic suffix tasks: Suffix Array / LCP hot sheet
KMP¶
pi[i] = longest proper prefix that is also a suffix for s[0..i]
Z-Function¶
z[i] = longest prefix match starting at i
Hashing Checklist¶
- normalize substring hashes
- use double hash or strong 64-bit style hashing
- remember it is probabilistic
Trie Checklist¶
- pass count
- end count
- alphabet representation
Core Invariants¶
- KMP / prefix function: longest proper border of each prefix
- Z-function: prefix match length starting at each position
- rolling hash: equal normalized hashes are evidence, not proof, unless the method is deterministic
- trie-based methods: node meaning must stay explicit
- regex / NFA methods: the active state set must always mean the full epsilon-closed reachable set after the current prefix
Main Trap¶
The classic string failure is choosing a heavier structure than the task needs, or using hashing where deterministic matching would already be simpler and safer.
Reopen Paths¶
- topic pages -> KMP, Z-Function, Palindromes / Manacher, Eertree / Palindromic Tree, Hashing, Aho-Corasick, Regular Expressions / Finite Automata, Suffix Tree, Suffix Array And LCP, Suffix Automaton
- exact quick sheets -> Palindromes hot sheet, Eertree hot sheet, String Hashing hot sheet, Aho-Corasick hot sheet, Regex / Finite Automata hot sheet, Suffix Tree hot sheet, Suffix Array / LCP hot sheet
- template layer -> Template library
- repo anchors -> String Matching, String Functions, Longest Palindrome, Distinct Palindromic Substrings, Finding Patterns, Regex Membership