Boyer-Moore Algorithm
One of the most efficient string searching algorithms in practice, using two heuristics: bad character rule and good suffix rule. Developed by Robert S. Boyer and J Strother Moore in 1977, it's the standard benchmark for practical string search, often outperforming other algorithms by skipping sections of text.
Visualization
Interactive visualization for Boyer-Moore Algorithm
Boyer-Moore Algorithm
Text:
• Time: O(n/m) best case, O(nm) worst
• Scans from right to left
• Very efficient in practice - industry standard
Interactive visualization with step-by-step execution
Implementation
1function boyerMoore(text: string, pattern: string): number[] {
2 const result: number[] = [];
3 const m = pattern.length;
4 const n = text.length;
5
6 if (m === 0) return result;
7
8 const badChar = buildBadCharTable(pattern);
9 let shift = 0;
10
11 while (shift <= n - m) {
12 let j = m - 1;
13
14 while (j >= 0 && pattern[j] === text[shift + j]) {
15 j--;
16 }
17
18 if (j < 0) {
19 result.push(shift);
20 shift += (shift + m < n) ? m - badChar.get(text[shift + m]) || m : 1;
21 } else {
22 shift += Math.max(1, j - (badChar.get(text[shift + j]) || -1));
23 }
24 }
25
26 return result;
27}
28
29function buildBadCharTable(pattern: string): Map<string, number> {
30 const table = new Map<string, number>();
31 for (let i = 0; i < pattern.length; i++) {
32 table.set(pattern[i], i);
33 }
34 return table;
35}Deep Dive
Theoretical Foundation
Boyer-Moore scans pattern from right to left but shifts pattern from left to right. It uses preprocessing to create bad character and good suffix tables that allow large jumps when mismatches occur.
Complexity
Time
O(n/m)
O(n)
O(nm)
Space
O(k) where k is alphabet size
Applications
Use Cases
Related Algorithms
Knuth-Morris-Pratt (KMP) Algorithm
An efficient string pattern matching algorithm that searches for occurrences of a 'word' within a 'text' by employing the observation that when a mismatch occurs, the word itself embodies sufficient information to determine where the next match could begin. Developed by Donald Knuth, Vaughan Pratt, and James H. Morris in 1977, it's one of the most important string algorithms with O(n+m) time complexity.
Rabin-Karp Algorithm
A string-searching algorithm that uses hashing to find pattern(s) in a text. Developed by Michael O. Rabin and Richard M. Karp in 1987, it's particularly useful for multiple pattern search and plagiarism detection. Uses rolling hash for efficiency.
Aho-Corasick Algorithm
A string-searching algorithm for locating elements of a finite set of strings (dictionary) within an input text. Invented by Alfred V. Aho and Margaret J. Corasick in 1975, it's a kind of dictionary-matching algorithm that simultaneously searches for all patterns in linear time, making it extremely efficient for multiple pattern matching.
Manacher's Algorithm
An optimal algorithm for finding the longest palindromic substring in linear time. Invented by Glenn K. Manacher in 1975, it cleverly avoids redundant comparisons by utilizing previously computed palindrome information, achieving O(n) time complexity which is optimal for this problem.