Overview
Comprehensive string algorithms for competitive programming including pattern matching (KMP, Rabin-Karp, Aho-Corasick), string hashing, suffix arrays, and palindrome detection.Pattern Matching Algorithms
KMP (Knuth-Morris-Pratt)
Efficient pattern matching algorithm that finds all occurrences of a pattern in text.Text to search in (string, vector, etc.)
Pattern to search for
vector<int> - Starting indices of all pattern occurrences
Complexity:
- Time: O(n + m) where n = text length, m = pattern length
- Space: O(m)
Prefix Function
Computes the longest proper prefix which is also suffix for each position.vector<int> - LPS (Longest Prefix Suffix) array
Complexity:
- Time: O(n)
- Space: O(n)
Z-Algorithm
Computes Z-array: length of longest substring starting from position i that matches prefix.vector<int> - Z-array where z[i] is the length of longest substring starting at i that matches prefix
Complexity:
- Time: O(n)
- Space: O(n)
Rabin-Karp
Rolling hash-based pattern matching algorithm.- Time: O(n + m) average, O(nm) worst case
- Space: O(1)
Aho-Corasick Algorithm
Multi-pattern matching algorithm that finds all occurrences of multiple patterns simultaneously.Counter Version
Counts total occurrences of all patterns.Occurrences Version
Finds all positions where patterns occur.- Build: O(sum of pattern lengths)
- Query: O(text length + number of matches)
- Space: O(ALPHABET_SIZE × total pattern length)
String Hashing
Rolling hash for fast substring comparison.Static Hashing
Get hash of substring [left, right] inclusive
- Build: O(n)
- Query: O(1)
- Space: O(n)
Dynamic Hashing
Supports string modifications. Complexity:- Update: O(log n)
- Query: O(log n)
Manacher’s Algorithm
Finds all palindromic substrings in linear time.Returns array where res[i] is the radius of palindrome centered at position i
Returns all palindromic substrings
- Time: O(n)
- Space: O(n)
Suffix Array
Sorts all suffixes of a string lexicographically.vector<int> - Indices of suffixes in sorted order
Complexity:
- Time: O(n log n)
- Space: O(n)
Minimum Expression
Finds lexicographically smallest rotation of a string.- Time: O(n)
- Space: O(1)
String Utilities
Split String
Available Algorithms
| Algorithm | File | Time Complexity |
|---|---|---|
| KMP | string_kmp_std.cpp | O(n+m) |
| Prefix Function | string_prefix_function.cpp | O(n) |
| Z-Algorithm | string_z_algorithm_std.cpp | O(n) |
| Z-Algorithm (Compress) | string_z_algorithm_compress.cpp | O(n) |
| Rabin-Karp | string_rabin_karp_std.cpp | O(n+m) avg |
| Aho-Corasick Counter | string_aho_corasick_counter.cpp | O(n+m) |
| Aho-Corasick Occurrences | string_aho_corasick_occurrences.cpp | O(n+matches) |
| Static Hashing | string_hashing_static_compress.cpp | O(n) build, O(1) query |
| Dynamic Hashing | string_hashing_dynamic_compress.cpp | O(log n) |
| Manacher | string_manacher.cpp | O(n) |
| Suffix Array | string_suffix_array.cpp | O(n log n) |
| Minimum Expression | string_minimum_expression.cpp | O(n) |
| Split | string_split.cpp | O(n) |
Common Use Cases
Pattern Matching
Single Pattern
Use KMP or Z-algorithm for single pattern matching
Multiple Patterns
Use Aho-Corasick for multiple pattern matching
Substring Comparison
Use hashing for fast substring equality checks
Palindromes
Use Manacher’s algorithm for all palindromes
See Also
Data Structures
Tries and other string data structures
Techniques
Two pointers and sliding window techniques