-
Notifications
You must be signed in to change notification settings - Fork 19.7k
/
Copy pathRabinKarpAlgorithm.java
68 lines (56 loc) · 2.44 KB
/
RabinKarpAlgorithm.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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 index = -1; // -1 here represents not found
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
// leading digit, add trailing digit
if (i < textLength - patternLength) {
hashForText = (ALPHABET_SIZE * (hashForText - text.charAt(i) * h) + text.charAt(i + patternLength)) % primeNumber;
// handling negative hashForText
if (hashForText < 0) {
hashForText = (hashForText + primeNumber);
}
}
}
return index; // return -1 if pattern does not found
}
}
// This code is contributed by nuclode