Introduction
String algorithms are essential in competitive programming for efficiently processing and matching text patterns. These algorithms enable fast substring searches, comparisons, and transformations.Pattern Matching
KMP, Rabin-Karp, Aho-Corasick, and Z-algorithm for finding patterns
String Hashing
Fast string comparison and substring matching using hash functions
Suffix Array
Efficient string indexing for pattern matching and LCP computation
Algorithm Comparison
Pattern Matching Algorithms
| Algorithm | Preprocessing | Search | Space | Multiple Patterns | Use Case |
|---|---|---|---|---|---|
| KMP | O(m) | O(n) | O(m) | ❌ No | Single pattern matching |
| Rabin-Karp | O(m) | O(n+m) avg | O(1) | ✅ Yes | Multiple pattern matching |
| Z-Algorithm | - | O(n+m) | O(n+m) | ❌ No | Pattern matching, palindromes |
| Aho-Corasick | O(Σm) | O(n+k) | O(Σm) | ✅ Yes | Multiple pattern matching |
Notation:
- n = length of text
- m = length of pattern
- k = number of matches
- Σm = sum of all pattern lengths
String Comparison Methods
| Method | Preprocessing | Comparison | Update | Best For |
|---|---|---|---|---|
| Direct Compare | O(1) | O(n) | O(1) | Short strings |
| Static Hashing | O(n) | O(1) | ❌ No | Read-only strings |
| Dynamic Hashing | O(n log n) | O(1) | O(log n) | Mutable strings |
| Suffix Array | O(n log n) | O(m log n) | ❌ No | Many pattern searches |
Choosing the Right Algorithm
Pattern Matching
Use KMP when:- Searching for a single pattern
- Need deterministic O(n) time
- Pattern has repeating prefixes
- Want simple, reliable code
- Searching for multiple patterns of same length
- Need simple implementation
- Can tolerate probabilistic approach
- Working with rolling hash problems
- Need all prefix matches
- Solving palindrome problems
- Want simple O(n) pattern matching
- Searching for multiple patterns simultaneously
- Patterns have different lengths
- Need to find all occurrences
- Dictionary matching problems
String Comparison
Use Static Hashing when:- Strings don’t change
- Need O(1) substring comparison
- Working with substring equality
- Solving string matching problems
- Strings are modified frequently
- Need O(1) comparison after updates
- Building strings dynamically
- Maintaining string in data structure
- Finding all occurrences of patterns
- Need lexicographic ordering
- Computing longest common substrings
- Working with multiple pattern searches
Common Patterns
Finding All Occurrences
- KMP
- Rabin-Karp
- Z-Algorithm
Substring Comparison
Multiple Pattern Matching
Advanced Techniques
Palindrome Detection
Longest Common Substring
String Period Detection
Common Applications
Text Search
Find patterns in large texts efficiently
DNA Matching
Match genetic sequences and find mutations
Plagiarism Detection
Find common substrings between documents
Data Compression
Identify repeated patterns for compression
Spell Checking
Dictionary matching with fuzzy search
URL Filtering
Match URLs against pattern lists
Performance Tips
- Use hashing for equality checks: O(1) after O(n) preprocessing
- Combine algorithms: Use hashing + binary search for complex queries
- Choose right base and mod: Avoid collisions in hashing
- Use double hashing: Reduce false positives
- Cache preprocessing: Reuse prefix functions and hash tables
- Consider memory: Aho-Corasick uses more space than KMP
Next Steps
Dive into specific implementations:- Pattern Matching - KMP, Rabin-Karp, Aho-Corasick, Z-algorithm
- String Hashing - Static and dynamic hashing techniques
- Suffix Array - Efficient string indexing