Visualize & Master
Algorithms & Data Structures
Explore classic & modern sorting algorithms, efficient searching techniques, and interactive data structure visualizations — all with real-time step-by-step animation, comparisons, swaps, and Big-O metrics.
Knuth-Morris-Pratt
Select an algorithm and press Play to watch the pattern slide across the text.
String Algorithms Algorithm Catalog
4 algorithms in this category. Click any card for detailed analysis.
Rabin-Karp
UnstableRabin-Karp hashes the pattern and every length-m window of the text using a rolling hash, comparing hashes first. Only when a hash collision occurs does it verify the actual characters, so most windows are skipped in constant time.
Knuth-Morris-Pratt
UnstableKMP finds all occurrences of a pattern in a text in linear time by precomputing an LPS (longest proper prefix-suffix) table. When a mismatch occurs, the pattern shifts by a known safe amount instead of restarting the comparison.
Z-Algorithm
UnstableThe Z-algorithm builds a Z-array over "pattern$text": z[i] is the length of the longest substring starting at i that matches a prefix of the string. Every position where z[i] equals the pattern length marks an occurrence.
Boyer-Moore
UnstableBoyer-Moore scans the pattern right-to-left and shifts the window using the last-occurrence (bad-character) rule and the good-suffix rule. When the text alphabet is large, most characters fail on the first probe and the pattern jumps quickly.
Complexity & Performance Tradeoffs
Side-by-side comparison of Big-O time and space complexity characteristics across string algorithms.
| Algorithm | Best Time | Average Time | Worst Time | Space Complexity | Stability |
|---|---|---|---|---|---|
| Knuth-Morris-Pratt | O(n) | O(n) | O(n + m) | O(m) | Unstable |
| Rabin-Karp | O(n + m) | O(n + m) | O(n·m) | O(1) | Unstable |
| Z-Algorithm | O(n) | O(n) | O(n) | O(n) | Unstable |
| Boyer-Moore | O(n/m) | O(n) | O(nm) | O(m) | Unstable |