Bölüm 5 · Simülatör

String Matching Algorithms

← Bölüm 5

Press Prepare to start.

Matches

Not available yet.

Preprocessing

The selected algorithm's table will appear here.

    String Matching Algorithms

    Compare Naive, KMP, Boyer-Moore, and Rabin-Karp step by step on the same text and pattern.

    How to use

    • Enter a text (up to 24 characters) and a pattern (up to 12 characters), then choose an algorithm.
    • Prepare generates all steps; Next Step advances one step, Auto Run plays the rest, Reset clears the run.
    • The pattern row is drawn under the current alignment (shift). Green: the compared characters match; red: mismatch; blue: an occurrence was found.
    • The Preprocessing panel shows the table the algorithm uses: LPS for KMP, the last-occurrence table for Boyer-Moore, hash values for Rabin-Karp.

    Naive

    Tries every shift from 0 to n − m and compares characters left to right until a mismatch.

    Knuth-Morris-Pratt (KMP)

    The LPS (longest proper prefix that is also a suffix) table tells how far the pattern can fall back after a mismatch, so the text index never moves backward.

    Boyer-Moore (bad-character rule)

    Compares right to left. On a mismatch the pattern is shifted so that the last occurrence of the mismatched text character lines up with it (at least one position).

    Rabin-Karp

    Compares a rolling hash of each text window with the pattern hash (base 256, modulus 101). Only windows with equal hashes are verified character by character, which filters out spurious hits.

    Complexity

    AlgorithmPreprocessingMatching (worst)
    Naive—O(n·m)
    KMPO(m)O(n)
    Boyer-Moore (bad character)O(m + σ)O(n·m), often sublinear in practice
    Rabin-KarpO(m)O(n + m) expected, O(n·m) worst