DryRun
AlgorithmsCourseCompare
DryRun
AlgorithmsCourseCompareGitHubLinkedIn

© 2026 DryRun. Built by Aditya Pandey.

AlgorithmsAdvancedBoyer-Moore
Advancedadvancedstringpattern-matchingheuristics

Boyer-Moore

Boyer-Moore compares pattern characters from right to left. It uses precomputed heuristics (bad character and good suffix) to skip ahead significantly, making it very fast in practice on long texts.

Complexity

Best
O(n/m)
Average
O(n)
Worst
O(nm)
Space
O(sigma)

Implementation

vector<int> boyerMoore(string t, string p) {
    const int SIG = 256;
    vector<int> last(SIG, -1), res;
    for (int i = 0; i < (int)p.size(); i++) last[(unsigned char)p[i]] = i;
    int n = t.size(), m = p.size(), s = 0;
    while (s <= n - m) {
        int j = m - 1;
        while (j >= 0 && p[j] == t[s + j]) j--;
        if (j < 0) {
            res.push_back(s);
            s += (s + m < n) ? m - last[(unsigned char)t[s + m]] : 1;
        } else {
            s += max(1, j - last[(unsigned char)t[s + j]]);
        }
    }
    return res;
}

How It Works

1.Right-to-Left Check

Mismatches near the end produce bigger shifts than left-to-right methods.

2.Bad Character Rule

Uses last occurrence map to decide shift distance quickly.

3.Performance

Excellent on large alphabets and long patterns in practice.

Related Algorithms

CpuKMP (Knuth-Morris-Pratt)CpuRabin-Karp

Test Your Knowledge

Quiz coming soon for this algorithm.

Back to all algorithms