Begin Algo
Algorithms · 6 lessons

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 up
Ctrl+F and grep

Finding 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: KMP
Plagiarism and duplicate detection

Hash each passage to a number and compare numbers instead of text. Rabin-Karp's rolling hash means sliding the window costs nothing.

→ Lesson: Rabin-Karp
DNA sequence analysis

A genome is a very long ACGT string. Finding a particular fragment, or a palindromic structure (restriction sites often are), is string algorithms.

→ Lesson: String Hashing
Matching thousands of keywords at once

A trie layers every keyword on top of each other, so one pass over the document finds all of them.

→ Lesson: Trie Applications

Lessons

6 lessons