String Matching Algorithm Simulator
Compare four exact string-matching methods with Unicode traces showing preprocessing and shifts plus overlap and hash-collision handling.{{ summaryTitle }}
{{ summaryLine }}
{{ primaryCopyAnnouncement }}
The chart renderer is unavailable. The same counts remain available in the exported CSV and summary.
| Step | Decision | Alignment | Compared pair | Explanation | Copy |
|---|---|---|---|---|---|
| {{ row.step }} | {{ row.decision }} | {{ row.alignment }} | {{ row.pair }} | {{ row.explanation }} |
Exact string matching looks for every position where a pattern appears as a contiguous part of a longer text. The result depends on literal character order, not on word meaning. Case changes, accents, normalization differences, and invisible characters can therefore turn text that looks similar into a different sequence.
A simple matcher tests the pattern at every possible alignment. Faster methods avoid some repeated work by learning from the pattern or from a compact summary of the current text window. Knuth–Morris–Pratt (KMP) reuses matched prefixes, Rabin–Karp filters windows with a rolling hash, and Boyer–Moore–Horspool compares from the pattern's right edge and shifts past unpromising alignments.
- Alignment
- A possible starting position for the pattern within the text.
- Preprocessing
- Work performed on the pattern before or alongside the main comparisons, such as an LPS or shift table.
- Fallback
- A KMP move to a shorter reusable prefix after a mismatch or completed match.
- Hash collision
- A Rabin–Karp window with the same hash as the pattern but different literal code points.
Overlapping matches are valid. In BANANA, the pattern ANA starts at code-point indices 1 and 3. Recording the first match must not skip the second alignment, so the fallback or shift rule after a match matters as much as mismatch handling.
Unicode code points provide a more useful unit than UTF-16 code units for an educational trace, but a code point is not always a complete user-perceived character. A letter plus a combining mark can occupy two code points, and visually identical text can use different normalization forms. Literal matching does not correct those differences.
Operation counts describe these specific trace rules and inputs. They do not measure elapsed time, allocation cost, optimized library behavior, processor effects, or the cost of preparing text for a production search.
How to Use This Tool:
Use the same literal text and pattern when comparing methods; only the algorithm should change.
- Choose Naive, KMP, Rabin–Karp, or Boyer–Moore–Horspool under Algorithm.
- Enter 0 to 80 Unicode code points in Text. An empty text is a valid no-match case.
- Enter a nonempty Pattern of 1 to 20 code points. Matching is literal and case-sensitive; overlapping starts are retained.
- Step through or play the trace. Follow the current alignment, compared text and pattern indices, equality result, preprocessing entries, fallback, shift, or hash-verification decision.
- Read the final match starts and then compare the work profile. Keep text, pattern, and character form unchanged so differences come from the algorithms rather than different inputs.
Interpreting Results:
Match starts are zero-based code-point indices. A result of No literal match means no complete alignment matched exactly; it does not mean the words are unrelated or that a case-insensitive, normalized, locale-aware, or regular-expression search would also fail.
Search comparisons, preprocessing comparisons, shifts, fallbacks, hash candidates, verified candidates, and collisions measure different kinds of work. Do not add them casually into a universal speed score. KMP may spend more work preprocessing, while Rabin–Karp may inspect many hashes but perform few literal comparisons.
A Rabin–Karp hash agreement is only a candidate. The literal verification decides whether the alignment is a match; collision evidence shows why the hash alone cannot be trusted.
Technical Details:
The text and pattern are split into Unicode code points and compared without case folding or normalization. Every algorithm reports all literal match starts under the same indexing rule, which makes their traces comparable even though their preprocessing and shift decisions differ.
Transformation Core:
| Algorithm | Prepared evidence | Search direction and shift rule | Match handling |
|---|---|---|---|
| Naive | No table | Compare left to right; after each alignment, shift by 1. | Record a complete alignment and continue, preserving overlaps. |
| KMP | Longest proper prefix that is also a suffix (LPS) length at each pattern index | Compare left to right; on mismatch, reuse the longest valid prefix instead of restarting the text scan. | After a match, fall back through the LPS value so overlaps remain possible. |
| Rabin–Karp | Pattern hash, rolling text-window hash, base 257, and modulus 101 | Shift by 1 while updating the window hash from the outgoing and incoming code points. | Verify every equal-hash window literally; reject unequal content as a collision. |
| Boyer–Moore–Horspool | Shift distance for code points before the pattern's final position | Compare right to left; shift by the distance assigned to the current window-end code point, or by the full pattern length when absent. | Record a full right-to-left match, then apply the same window-end shift rule. |
Formula Core:
Rabin–Karp maps each code point to its numeric value plus 1, then evaluates a polynomial hash. For a pattern or window of length m, base b is 257 and modulus q is 101.
The next window removes the outgoing code point's highest-place contribution, multiplies by the base, and adds the incoming code point. Modulo correction keeps the intermediate value non-negative.
Because the modulus is deliberately small enough to make collisions visible in a teaching trace, equal hashes never bypass literal comparison.
Boundary Rules:
- If the pattern is longer than the text, no candidate alignment exists.
- An empty text with a nonempty pattern is valid and produces no match.
- An empty pattern is invalid rather than being treated as a match at every boundary.
- Text is limited to 80 code points and the pattern to 20 code points so traces remain bounded.
- Playback interval changes animation timing only; it does not change matches or operation counts.
Worked Examples:
Overlapping matches
With text BANANA and pattern ANA, the complete literal matches start at code-point indices 1 and 3. KMP's post-match fallback and the other algorithms' continuing shifts preserve the second, overlapping result.
Emoji code-point indexing
With text A😀B😀A😀 and pattern 😀A, the match starts at code-point index 3. UTF-16-based indexes in another program may differ because each emoji can occupy a surrogate pair there.
References:
- Fast Pattern Matching in Strings, SIAM Journal on Computing, June 1977.
- Karp–Rabin, NIST Dictionary of Algorithms and Data Structures.
- Practical fast searching in strings, Software: Practice and Experience, June 1980.
- The Unicode Standard, Version 17.0.0, The Unicode Consortium, 2025.