#### Rabin Karp Algorithm

d
```package com.thealgorithms.searches;

// Implementation of Rabin Karp Algorithm

public final class RabinKarpAlgorithm {
private RabinKarpAlgorithm() {
}

private static final int ALPHABET_SIZE = 256;

public static int search(String pattern, String text, int primeNumber) {

int patternLength = pattern.length();
int textLength = text.length();
int hashForPattern = 0;
int hashForText = 0;
int h = 1;

// The value of h would be "pow(d, patternLength-1)%primeNumber"
for (int i = 0; i < patternLength - 1; i++) h = (h * ALPHABET_SIZE) % primeNumber;

// Calculate the hash value of pattern and first
// window of text
for (int i = 0; i < patternLength; i++) {
hashForPattern = (ALPHABET_SIZE * hashForPattern + pattern.charAt(i)) % primeNumber;
hashForText = (ALPHABET_SIZE * hashForText + text.charAt(i)) % primeNumber;
}

// Slide the pattern over text one by one
for (int i = 0; i <= textLength - patternLength; i++) {
/* Check the hash values of current window of text
and pattern. If the hash values match then only
check for characters one by one*/

int j = 0;
if (hashForPattern == hashForText) {
/* Check for characters one by one */
for (j = 0; j < patternLength; j++) {
if (text.charAt(i + j) != pattern.charAt(j)) break;
}

// if hashForPattern == hashForText and pattern[0...patternLength-1] = text[i, i+1, ...i+patternLength-1]
if (j == patternLength) {
index = i;
return index;
}
}

// Calculate hash value for next window of text: Remove