String AlgorithmsMatching, searching and hashing
Brute-force string matching is O(nm) in the worst case. Every algorithm in this topic avoids re-comparing in a different way: hashing replaces strings with numbers, KMP and Z reuse what has already been matched, and Manacher exploits the symmetry of palindromes.
Why learn String Algorithms
Where it shows upFinding a string in a hundred-million-character log is O(nm) at worst by brute force. KMP never re-reads what it has already matched, making it linear.
→ Lesson: KMPHash each passage to a number and compare numbers instead of text. Rabin-Karp's rolling hash means sliding the window costs nothing.
→ Lesson: Rabin-KarpA genome is a very long ACGT string. Finding a particular fragment, or a palindromic structure (restriction sites often are), is string algorithms.
→ Lesson: String HashingA trie layers every keyword on top of each other, so one pass over the document finds all of them.
→ Lesson: Trie Applications