Skip to content

Commit 4fab7ad

Browse files
asapekiaarintripathi1vil02
authored
code-clean-up (TheAlgorithms#4519)
* code-clean-up * style: make `RabinKarpAlgorithm` a proper utility class --------- Co-authored-by: arintripathi1 <arint@trainee.nrifintech.com> Co-authored-by: vil02 <vil02@o2.pl> Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com>
1 parent 535230a commit 4fab7ad

File tree

2 files changed

+33
-37
lines changed

2 files changed

+33
-37
lines changed

src/main/java/com/thealgorithms/searches/RabinKarpAlgorithm.java

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,60 @@
11
package com.thealgorithms.searches;
22

3-
// Following program is a Java implementation
4-
// of Rabin Karp Algorithm given in the CLRS book
3+
// Implementation of Rabin Karp Algorithm
54

6-
public class RabinKarpAlgorithm {
5+
public final class RabinKarpAlgorithm {
6+
private RabinKarpAlgorithm() {
7+
}
78

89
// d is the number of characters in the input alphabet
9-
public static final int d = 256;
10+
private static final int d = 256;
11+
12+
public static int search(String pattern, String text, int primeNumber) {
1013

11-
/* pat -> pattern
12-
txt -> text
13-
q -> A prime number
14-
*/
15-
public int search(String pat, String txt, int q) {
16-
int index = -1; // note: -1 here represent not found, it is not an index
17-
int M = pat.length();
18-
int N = txt.length();
19-
int i, j;
20-
int p = 0; // hash value for pattern
21-
int t = 0; // hash value for txt
14+
int index = -1; // -1 here represents not found
15+
int patternLength = pattern.length();
16+
int textLength = text.length();
17+
int hashForPattern = 0;
18+
int hashForText = 0;
2219
int h = 1;
2320

24-
// The value of h would be "pow(d, M-1)%q"
25-
for (i = 0; i < M - 1; i++) h = (h * d) % q;
21+
// The value of h would be "pow(d, patternLength-1)%primeNumber"
22+
for (int i = 0; i < patternLength - 1; i++) h = (h * d) % primeNumber;
2623

2724
// Calculate the hash value of pattern and first
2825
// window of text
29-
for (i = 0; i < M; i++) {
30-
p = (d * p + pat.charAt(i)) % q;
31-
t = (d * t + txt.charAt(i)) % q;
26+
for (int i = 0; i < patternLength; i++) {
27+
hashForPattern = (d * hashForPattern + pattern.charAt(i)) % primeNumber;
28+
hashForText = (d * hashForText + text.charAt(i)) % primeNumber;
3229
}
3330

3431
// Slide the pattern over text one by one
35-
for (i = 0; i <= N - M; i++) {
36-
// Check the hash values of current window of text
37-
// and pattern. If the hash values match then only
38-
// check for characters one by one
39-
if (p == t) {
32+
for (int i = 0; i <= textLength - patternLength; i++) {
33+
/* Check the hash values of current window of text
34+
and pattern. If the hash values match then only
35+
check for characters one by one*/
36+
37+
int j = 0;
38+
if (hashForPattern == hashForText) {
4039
/* Check for characters one by one */
41-
for (j = 0; j < M; j++) {
42-
if (txt.charAt(i + j) != pat.charAt(j)) break;
40+
for (j = 0; j < patternLength; j++) {
41+
if (text.charAt(i + j) != pattern.charAt(j)) break;
4342
}
4443

45-
// if p == t and pat[0...M-1] = txt[i, i+1, ...i+M-1]
46-
if (j == M) {
47-
System.out.println("Pattern found at index " + i);
44+
// if hashForPattern == hashForText and pattern[0...patternLength-1] = text[i, i+1, ...i+patternLength-1]
45+
if (j == patternLength) {
4846
index = i;
4947
return index;
5048
}
5149
}
5250

5351
// Calculate hash value for next window of text: Remove
5452
// leading digit, add trailing digit
55-
if (i < N - M) {
56-
t = (d * (t - txt.charAt(i) * h) + txt.charAt(i + M)) % q;
53+
if (i < textLength - patternLength) {
54+
hashForText = (d * (hashForText - text.charAt(i) * h) + text.charAt(i + patternLength)) % primeNumber;
5755

58-
// We might get negative value of t, converting it
59-
// to positive
60-
if (t < 0) t = (t + q);
56+
// handling negative hashForText
57+
if (hashForText < 0) hashForText = (hashForText + primeNumber);
6158
}
6259
}
6360
return index; // return -1 if pattern does not found

src/test/java/com/thealgorithms/searches/RabinKarpAlgorithmTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66
import org.junit.jupiter.params.provider.CsvSource;
77

88
class RabinKarpAlgorithmTest {
9-
RabinKarpAlgorithm RKA = new RabinKarpAlgorithm();
109

1110
@ParameterizedTest
1211
@CsvSource({"This is an example for rabin karp algorithmn, algorithmn, 101", "AAABBDDG, AAA, 137", "AAABBCCBB, BBCC, 101", "AAABBCCBB, BBCC, 131", "AAAABBBBCCC, CCC, 41", "ABCBCBCAAB, AADB, 293", "Algorithm The Algorithm, Algorithm, 101"})
1312
void RabinKarpAlgorithmTestExample(String txt, String pat, int q) {
14-
int indexFromOurAlgorithm = RKA.search(pat, txt, q);
13+
int indexFromOurAlgorithm = RabinKarpAlgorithm.search(pat, txt, q);
1514
int indexFromLinearSearch = txt.indexOf(pat);
1615
assertEquals(indexFromOurAlgorithm, indexFromLinearSearch);
1716
}

0 commit comments

Comments
 (0)