Skip to content

Commit 7e7a618

Browse files
renamed method variables for improved readability.
1 parent d636d56 commit 7e7a618

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

src/main/java/com/thealgorithms/strings/RabinKarp.java

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,57 +16,57 @@ private RabinKarp() {
1616
private static final int ALPHABET_SIZE = 256;
1717

1818
public static List<Integer> search(String text, String pattern) {
19-
return search(text, pattern, 101);
19+
return search(text, pattern, 101); // 101 is a prime number used as modulo
2020
}
2121

22-
public static List<Integer> search(String text, String pattern, int q) {
22+
public static List<Integer> search(String text, String pattern, int primeModulo) {
2323
List<Integer> occurrences = new ArrayList<>();
2424
if (text == null || pattern == null || pattern.isEmpty()) {
2525
return occurrences;
2626
}
2727

28-
int m = pattern.length();
29-
int n = text.length();
30-
int t = 0;
31-
int p = 0;
32-
int h = 1;
33-
int j = 0;
34-
int i = 0;
28+
int patternLength = pattern.length();
29+
int textLength = text.length();
30+
int textHash = 0;
31+
int patternHash = 0;
32+
int highestPowerHash = 1;
33+
int matchIndex;
34+
int currentIndex;
3535

36-
if (m > n) {
36+
if (patternLength > textLength) {
3737
return new ArrayList<>();
3838
}
3939

40-
// h = pow(ALPHABET_SIZE, m-1) % q
41-
for (i = 0; i < m - 1; i++) {
42-
h = h * ALPHABET_SIZE % q;
40+
// highestPowerHash = pow(ALPHABET_SIZE, patternLength-1) % primeModulo
41+
for (currentIndex = 0; currentIndex < patternLength - 1; currentIndex++) {
42+
highestPowerHash = highestPowerHash * ALPHABET_SIZE % primeModulo;
4343
}
4444

45-
for (i = 0; i < m; i++) {
46-
p = (ALPHABET_SIZE * p + pattern.charAt(i)) % q;
47-
t = (ALPHABET_SIZE * t + text.charAt(i)) % q;
45+
for (currentIndex = 0; currentIndex < patternLength; currentIndex++) {
46+
patternHash = (ALPHABET_SIZE * patternHash + pattern.charAt(currentIndex)) % primeModulo;
47+
textHash = (ALPHABET_SIZE * textHash + text.charAt(currentIndex)) % primeModulo;
4848
}
4949

50-
for (i = 0; i <= n - m; i++) {
51-
if (p == t) {
52-
for (j = 0; j < m; j++) {
53-
if (text.charAt(i + j) != pattern.charAt(j)) {
50+
for (currentIndex = 0; currentIndex <= textLength - patternLength; currentIndex++) {
51+
if (patternHash == textHash) {
52+
for (matchIndex = 0; matchIndex < patternLength; matchIndex++) {
53+
if (text.charAt(currentIndex + matchIndex) != pattern.charAt(matchIndex)) {
5454
break;
5555
}
5656
}
5757

58-
if (j == m) {
59-
occurrences.add(i);
58+
if (matchIndex == patternLength) {
59+
occurrences.add(currentIndex);
6060
}
6161
}
6262

63-
if (i < n - m) {
64-
t = (t - text.charAt(i) * h % q);
65-
if (t < 0) {
66-
t += q;
63+
if (currentIndex < textLength - patternLength) {
64+
textHash = (textHash - text.charAt(currentIndex) * highestPowerHash % primeModulo);
65+
if (textHash < 0) {
66+
textHash += primeModulo;
6767
}
68-
t = t * ALPHABET_SIZE % q;
69-
t = (t + text.charAt(i + m)) % q;
68+
textHash = textHash * ALPHABET_SIZE % primeModulo;
69+
textHash = (textHash + text.charAt(currentIndex + patternLength)) % primeModulo;
7070
}
7171
}
7272
return occurrences;

0 commit comments

Comments
 (0)