The Bitap Algorithm is an approximate string matching algorithm. The algorithm tells whether a given text contains a substring which is “approximately equal” to a given pattern. Here approximately equal states that if the substring and pattern are within a given distance k of each other. The algorithm begins by precomputing a set of bitmasks containing one bit for each element of the pattern. This does most of the bitwise operations, which are quick.
The Bitap Algorithm is also known as shift-or, shift-and, or Baeza Yates Gonnet Algorithm.
Input: Text: geeksforgeeks Pattern: geeks Output: Pattern found at index: 0 Input: Text: Youareawesome Pattern: Youareamazing Output: No Match.
- Input the text pattern as a string.
- We will convert this String to a simple Char Array
- If the length is 0 or exceeding 63, we return “No Match”.
- Now, by taking array R which complements 0.
- Taking an array “pattern_mask” which complements 0, to 1
- Taking the pattern as an index of “pattern_mask” then by using the and operator we and it with the result of the complement of 1L(long integer) by shifting it to left side by i times.
- Now by running the loop till text length.
- We now or it with R and pattern mask.
- Now by left shifting the 1L by the length of the pattern and then the result is an and with R
- If it is equal to zero we print it by I-len+1 else return -1
- Output is the index of the text, where the pattern matches.
Bitap Algorithm! Pattern found at index: 0
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.