Java Program to Implement Bitap Algorithm for String Matching

• Difficulty Level : Expert
• Last Updated : 15 Dec, 2021

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.

Example:

Input:
Text: geeksforgeeks
Pattern: geeks
Output:
Pattern found at index: 0

Input:
Text: Youareawesome
Pattern: Youareamazing
Output:
No Match.

Approach:

• 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.

Code: