What is a KMP String Matching Visualizer?
A KMP string matching visualizer shows how the Knuth-Morris-Pratt algorithm finds a pattern inside text without restarting the scan from scratch after every mismatch. It is useful for learning prefix functions, LPS tables, string searching, and coding interview pattern-matching problems.
This tool lets you enter text and a pattern, inspect the LPS table, then step through comparisons, fallbacks, and match indexes.
For the full algorithm set, browse the Data Structure Visualizers hub.
How to use this KMP visualizer
- Enter the text to scan.
- Enter the pattern to search for.
- Click Run KMP to jump to the completed match list.
- Step forward to watch text index
i, pattern indexj, matches, and LPS fallbacks. - Green text cells are completed matches, blue cells are the current comparison, and amber cells are the current aligned prefix.
KMP and the LPS table
KMP precomputes the longest proper prefix that is also a suffix for each pattern prefix. When a mismatch happens after several matched characters, the LPS value tells the algorithm where pattern index j can safely jump.
That fallback is the key difference from a naive string search. The text index does not need to move backward.
Compare this page with the Rabin Karp Visualizer to see string matching with rolling hashes instead of prefix fallback jumps.