Implementing Rabin Karp Algorithm Using Rolling Hash in Java
Last Updated :
02 Feb, 2022
There are so many pattern searching algorithms for the string. KMP algorithm, Z algorithm Rabin Karp algorithm, etc these algorithms are the optimization of Naive Pattern searching Algorithm.
Naive Pattern Searching Algorithm:
Input : "AABACACAACAC"
Pattern : "CAC"
Output : [4,9]
AABACACAACAC
Implementation:
Java
// Java Program to Search for a Pattern String in the Input
// String returning the indices
// Importing input java classes
import java.io.*;
// Main class
public class GFG {
// Method 1
// To find pattern in the string
static void search(String s, String pattern)
{
// Iterating over the string in which to be searched
// for using the length() method
for (int i = 0; i <= s.length() - pattern.length();
++i) {
int check = 1;
// Iterating over the string for which is to be
// searched using the length() method
for (int j = 0; j < pattern.length(); ++j) {
// Now, checking the elements of pattern
// with the given string using the charAt()
// method
if (s.charAt(i + j) != pattern.charAt(j)) {
// Setting check to zero as pattern is
// not detected here
check = 0;
// Break statement to hault
// execution of code
break;
}
}
// Now if the check remains same as declared
// then pattern is detected at least once
if (check == 1) {
// Printing the position(index) of the
// pattern string in the input string
System.out.print(i + " , ");
}
}
}
// Method 2
// Main driver method
public static void main(String[] args)
{
// Given custom input string
String s = "AABACACAACAC";
// Pattern to be looked after in
// the above input string
String pattern = "CAC";
// Display message for interpreting the indices
// ai where if the pattern exists
System.out.print(
"Pattern is found at the indices : ");
// Calling the above search() method
// in the main() method
search(s, pattern);
}
}
OutputPattern is found at the indices : 4 , 9 ,
Output explanation:
The above algorithm for pattern searching is the basic algorithm the worst as the average time complexity of this algorithm is O(n×m) where n is the pattern and m is the given string.
How can we reduce the complexity of this algorithm?
It is possible with the help of rolling hash. Rabin Karp algorithm is one of the optimized algorithms of the naive algorithm that performs searching by rolling over the string and search the pattern.
Illustration:
Input: txt[] = "THIS IS A TEST TEXT"
pat[] = "TEST"
Output: Pattern found at index 10
Input: txt[] = "AABAACAADAABAABA"
pat[] = "AABA"
Output: Pattern found at index 0
Pattern found at index 9
Pattern found at index 12
Procedure:
- Calculate the hash value of the pattern (By creating your own hash function or equation to determining an individual hash value for every character)
- Iterate over the string check the hash value of every substring generated of the size of the pattern if matched check every character of the pattern as well as String if all matched print the starting index of the string.
- If not matched shift to the next character by skipping the first character and adding the hash value of the next character that we don't calculate the hash value of the next substring in the string only we slide the window skip the first character and add the last character in the window by calculating its hash value i.e Rolling hash.
Implementation:
Simple Rolling algorithm assuming the pattern of length 2
- Initialize temp=4(1+3)
- Roll the hash value to the next element.
- Iterate the loop 'i' <= Array.length-pattern.length
- Remove the first element from the temp variable and add next element in the temp variable. temp = temp-Array[i]+Array[i.pattern.length()]
Java
// Java Program to illustrating Simple Rolling Hashing
// Importing input output classes
import java.io.*;
// Main class
class GFG {
// Method 1
// To search the pattern
static void search(String S, String pattern)
{
// Declaring and initializing the hash values
int hash1 = 0;
int hash2 = 0;
// Iterating over the pattern string to be matched
// over
for (int i = 0; i < pattern.length(); ++i) {
// Storing the hash value of the pattern
hash1 += pattern.charAt(i) - 'A';
// Storing First hash value of the string
hash2 += S.charAt(i) - 'A';
}
// Initially declaring with zero
int j = 0;
// Iterating over the pattern string to checkout
// hash values
for (int i = 0; i <= S.length() - pattern.length();
++i) {
// Checking the hash value
if (hash2 == hash1) {
// Checking the value
for (j = 0; j < pattern.length(); ++j) {
// Checking for detection of pattern in a
// pattern
if (pattern.charAt(j)
!= S.charAt(i + j)) {
// Break statement to hault the
// execution of program as no
// pattern found
break;
}
}
}
// If execution is not stopped means
// pattern(sub-string) is present
// So now simply detecting for one or more
// occurrences inbetween pattern string using the
// length() method
if (j == pattern.length()) {
// Pattern is detected so printing the index
System.out.println(i);
}
// for last case of loop, have to check,
// otherwise,
// S.charAt(i + pattern.length()) below will
// throw error
if (i == S.length() - pattern.length())
break;
// Roll the hash value over the string detected
hash2 = (int)((hash2) - (S.charAt(i) - 'A'))
+ S.charAt(i + pattern.length()) - 'A';
}
}
// Method 2
// Main driver method
public static void main(String[] args)
{
// Input string to be traversed
String S = "AABAACAADAABAABA";
// Pattern string to be checked
String pattern = "AABA";
// Calling the above search() method(Method1)
// in the main() method
search(S, pattern);
}
}
Similar Reads
How to Implement a Bidirectional Map Using Two HashSets in Java? Bidirectional maps are also known as two-way maps or dual maps. These provide a convenient way to establish a relationship between keys and values in both directions. In Java, we can implement these using HashSet. So, in this article, we will see how to implement bidirectional maps in Java using two
4 min read
Java Program to Implement HashTables with Linear Probing Hashing is a technique that is used to uniquely identify a specific object from a group of similar objects. Suppose an object is to be assigned a key to it to make searching easy. To store the key/value pair, one can use a simple array like a data structure where keys (integers) can be used directly
5 min read
Java Program to Implement HashTable API The Hashtable class implements a hash table, which maps keys to values. Any non-null object can be used as a key or as a value. To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method. To implement Hashtable API f
4 min read
Java Program to Implement ConcurrentHashMap API ConcurrentHashMap class obeys the same functional specification as HashTable and includes all the versions of methods corresponding to each method of a HashTable. A HashTable supports the full concurrency of retrievals and adjustable concurrency for updates. All the operations of ConcurrentHashMap a
6 min read
Implementation of Blockchain in Java Blockchain is the backbone Technology of Digital CryptoCurrency BitCoin. A Blockchain is a list of records called blocks that are linked together using linked lists and use the cryptographic technique.Each block contains its own digital fingerprint called Hash, the hash of the previous block, a tim
5 min read
Java Program to Implement Hash Tables with Double Hashing Double hashing is a technique in an open addressing scheme. and there is the ordinary hash function. In an open addressing scheme, the actual hash function is taking the ordinary hash function when its space is not empty then it will perform another hash function to get some space to insert. Double
10 min read
How to Implement a Custom Hash function for Keys in a HashMap in Java? In Java, HashMap is the data structure that implements the Map interface. This is used to save the data in the form of key-value pairs. In this article, we will learn how to implement a Custom Hash function for keys in a HashMap in Java. In Java, implementing the custom hash function for keys in a H
2 min read
Java Program to Implement Hash Tables Chaining with Doubly Linked Lists Hash Tables(similar to tables in general) provide a subset of the dynamic set operations. Usually, a set of keys are mapped with some values based on certain relations. However, there might be situations when different keys map to the same position provided by the Hash function, which leads to a col
5 min read
Java Program to Read Elements using Enumeration in Hashtable The enumeration in java is one of the predefined interfaces, whose object is used for retrieving the data from collections framework variable(like Stack, Vector, HashTable, etc.) in a forward direction only and not in the backward direction. HashTable is a class The hash table class implements a Map
3 min read
Hamming code Implementation in Java Pre-requisite: Hamming code Hamming code is a set of error-correction codes that can be used to detect and correct the errors that can occur when the data is moved or stored from the sender to the receiver. It is a technique developed by R.W. Hamming for error correction. Examples: Input: message bi
3 min read