What is the Boyer-Moore Algorithm?
Boyer-Moore searches for a pattern by comparing it against the text starting from the rightmost character instead of the leftmost. This flips the usual intuition: on a mismatch, the algorithm often already knows enough to skip multiple positions at once, rather than sliding forward by just one.
This demo implements the bad character rule: when a mismatch occurs, it looks up the last position of the mismatched text character within the pattern and shifts the pattern to align with it. The full algorithm also uses a good suffix rule for even larger jumps, giving Boyer- Moore its reputation as one of the fastest general-purpose string search algorithms in practice - often faster than KMP on natural-language text with a large alphabet.
Time & Space Complexity
- Best CaseO(n / m)
- Worst CaseO(n · m)
- Space (Bad Char Table)O(alphabet size)
* Sub-linear best case: not every text character needs to be inspected.
Real-World Use Cases
- ✓grep and Text EditorsMany Unix search tools use Boyer-Moore or its variants for fast literal search.
- ✓Antivirus Signature ScanningQuickly skipping over large files while searching for known byte signatures.
- ✓Large-Alphabet SearchPerforms best when the alphabet is large relative to the pattern length, like natural language text.