Bitap Algorithm (Shift-Or, Baeza-Yates-Gonnet)
Approximate string matching using bitwise operations. Fast for short patterns.
Visualization
Interactive visualization for Bitap Algorithm (Shift-Or, Baeza-Yates-Gonnet)
Bitap Algorithm:
- • Shift-Or/Shift-And algorithm
- • Bitwise pattern matching
Interactive visualization with step-by-step execution
Implementation
1function bitapSearch(text: string, pattern: string): number {
2 const m = pattern.length;
3 if (m > 31) return -1; // Pattern too long for 32-bit
4 const patternMask: Record<string, number> = {};
5 for (let i = 0; i < m; i++) {
6 const c = pattern[i];
7 patternMask[c] = (patternMask[c] || ~0) & ~(1 << i);
8 }
9 let R = ~0;
10 for (let i = 0; i < text.length; i++) {
11 R = (R << 1) | (patternMask[text[i]] ?? ~0);
12 if ((R & (1 << (m - 1))) === 0) return i - m + 1;
13 }
14 return -1;
15}Deep Dive
Theoretical Foundation
Uses bitmasks to track pattern matches. For each text char, shifts state and ORs with pattern mask. Very fast for patterns ≤ word size.
Complexity
Time
O(n)
O(n)
O(n)
Space
O(|Σ|)
Applications
Industry Use
Agrep (approximate grep)
Text editors
DNA searching
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.
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.
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.