BEGINNER · 10 MIN · FREE LESSON
Naive string matching
Explore naive string matching through a visual, step-by-step model.
Loading interactive lesson…
BEGINNER · 10 MIN · FREE LESSON
Explore naive string matching through a visual, step-by-step model.
Loading interactive lesson…
Enter {"text":"ababcabcabababd","pattern":"ababd"}. These matching models use printable ASCII; matches are zero-based and may overlap.
Find every complete pattern occurrence, including overlaps.
Compare from each possible alignment; repeated near-matches can redo m comparisons at almost every text position.
Measured reads + comparisons, seed 42. Plotted sample sizes respect each lesson’s explicit input limits.
Keyboard: Space play/pause · ←/→ step · R restart · 1–5 speed. Reduced motion disables transitions.
Slide a short stencil across a longer message until every character underneath agrees.
Try every alignment. Stop at the first mismatch. Use a repeated-character input and compare the resulting counters with a less repetitive example.
Predict a character comparison, run extension, palindrome expansion or merge frequency. Step back and explain which stored state avoided repeated work.
O(nm) worst time and O(1) auxiliary space. The player explains the relevant invariant and counts actual comparisons, reads and writes. Output storage is reported separately when it grows with the result.
The four references assume the lesson’s printable ASCII input contract. The foundation Strings lesson separately explains Unicode code points and UTF-16 units. Pattern inputs must be nonempty; text may be empty for matching. Huffman and palindrome inputs must be nonempty.
Use for short inputs, one-off searches and as a correctness baseline.
Do not skip overlapping occurrences or read beyond the final valid alignment.
Check the invariant, cost model and an edge condition below.
MAKE IT STICK
Progress saves on this device and merges with your account after sign-in.
0 / 5000 · Saved on this device; synced when signed in.