# String Matching Algorithms Confidence: high Last verified: 2026-05-22 Generation: human_only ## TL;DR String matching finds occurrences of a pattern in text. Naive: O(nm). KMP (Knuth-Morris-Pratt): O(n+m) using prefix function. Boyer-Moore: O(n/m) average, O(nm) worst. Rabin-Karp: O(n+m) average using rolling hash. Real-world text search uses hybrid algorithms. ## Core Explanation KMP precomputes failure function (longest proper prefix that is also suffix). Boyer-Moore scans pattern right-to-left, skipping characters — fastest in practice for English text. Aho-Corasick extends KMP for multiple pattern matching (used in fgrep, intrusion detection). Suffix arrays/trees enable O(m) substring search with O(n) preprocessing. ## Further Reading - [Introduction to Algorithms (CLRS)](undefined)