Java Program to Implement Bitap Algorithm for String Matching Last Updated : 22 Mar, 2023 Comments Improve Suggest changes Like Article Like Report 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 ArrayIf 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 1Taking 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 RIf it is equal to zero we print it by I-len+1 else return -1Output is the index of the text, where the pattern matches. Code: Java // Java Program to implement Bitap Algorithm. import java.io.*; import java.io.IOException; public class GFG { public static void main(String[] args) throws IOException { System.out.println("Bitap Algorithm!"); String text = "geeksforgeeks"; String pattern = "geeks"; // This is an object created of the class for // extension of functions. GFG g = new GFG(); // Now here we go to findPattern functions , we two // arguments. g.findPattern(text, pattern); } public void findPattern(String t, String p) { // we convert the String text to Character Array for // easy indexing char[] text = t.toCharArray(); // we convert the String pattern to Character Array // to access each letter in the String easily. char[] pattern = p.toCharArray(); // Index shows the function bitap search if they are // equal at a particular index or not int index = bitap_search(text, pattern); // If the pattern is not equal to the text of the // string approximately Then we tend to return -1 If // index is -1 Then we print there is No match if (index == -1) { System.out.println("\nNo Match\n"); } else { // Else if there is a match // Then we print the position of the index at // where the pattern and the text matches. System.out.println( "\nPattern found at index: \n" + index); } } private int bitap_search(char[] text, char[] pattern) { // Here the len variable is taken // This variable accepts the pattern length as its // value int len = pattern.length; // This is an array of pattern_mask of all // character values in it. long pattern_mask[] = new long[Character.MAX_VALUE + 1]; // Here the variable of being long type is // complemented with 1; long R = ~1; // Now if the length of the pattern is 0 // we would return -1 if (len == 0) { return -1; } // Or if the length of the pattern exceeds the // length of the character array Then we would // declare that the pattern is too long. We would // return -1 if (len > 63) { System.out.println("Pattern too long!"); return -1; } // Now filling the values in the pattern mask // We would run th eloop until the max value of // character Initially all the values of Character // are put up inside the pattern mask array And // initially they are complemented with zero for (int i = 0; i <= Character.MAX_VALUE; ++i) pattern_mask[i] = ~0; // Now len being the variable of pattern length , // the loop is set till there Now the pattern being // the index of the pattern_mask 1L means the long // integer is shifted to left by i times The result // of that is now being complemented the result of // the above is now being used as an and operator We // and the pattern_mask and the result of it for (int i = 0; i < len; ++i) pattern_mask[pattern[i]] &= ~(1L << i); // Now the loop is made to run until the text length // Now what we do this the R array is used // as an Or function with pattern_mask at index of // text of i for (int i = 0; i < text.length; ++i) { R |= pattern_mask[text[i]]; // Now result of the r after the above // operation // we shift it to left side by 1 time R <<= 1; // If the 1L long integer if shifted left of the // len And the result is used to and the result // and R array // If that result is equal to 0 // We return the index value // Index=i-len+1 if ((R & (1L << len)) == 0) return i - len + 1; } // if the index is not matched // then we return it as -1 // stating no match found. return -1; } } OutputBitap Algorithm! Pattern found at index: 0 Comment More infoAdvertise with us Next Article Java Program to Implement Bitap Algorithm for String Matching S saransh9342 Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Java-String-Programs +1 More Practice Tags : Java Similar Reads Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s 10 min read Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per 15+ min read Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it, 13 min read Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me 15+ min read Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an 13 min read Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac 15+ min read Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt 10 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact 12 min read Like