Skip to content

Commit 79f52ae

Browse files
author
Divyansh Saxena
committed
Running KMPTest and RabinKarpTest with fixed formatting
1 parent f2a5c1e commit 79f52ae

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,14 @@ public static List<Integer> search(String text, String pattern, int q) {
3333
int j = 0;
3434
int i = 0;
3535

36-
h = (int) Math.pow(ALPHABET_SIZE, m - 1) % q;
36+
if (m > n) {
37+
return new ArrayList<>();
38+
}
39+
40+
// h = pow(ALPHABET_SIZE, m-1) % q
41+
for (i = 0; i < m - 1; i++) {
42+
h = (h * ALPHABET_SIZE) % q;
43+
}
3744

3845
for (i = 0; i < m; i++) {
3946
p = (ALPHABET_SIZE * p + pattern.charAt(i)) % q;
@@ -54,10 +61,12 @@ public static List<Integer> search(String text, String pattern, int q) {
5461
}
5562

5663
if (i < n - m) {
57-
t = (ALPHABET_SIZE * (t - text.charAt(i) * h) + text.charAt(i + m)) % q;
64+
t = (t - (text.charAt(i) * h) % q);
5865
if (t < 0) {
59-
t = (t + q);
66+
t += q;
6067
}
68+
t = (t * ALPHABET_SIZE) % q;
69+
t = (t + text.charAt(i + m)) % q;
6170
}
6271
}
6372
return occurrences;

src/test/java/com/thealgorithms/strings/KMPTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thealgorithms.strings;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
45
import java.util.List;
56
import org.junit.jupiter.api.Test;
67

@@ -9,7 +10,7 @@ public class KMPTest {
910
@Test
1011
public void testKMPMatcher() {
1112
assertEquals(List.of(0, 1), KMP.kmpMatcher("AAAAABAAABA", "AAAA"));
12-
assertEquals(List.of(0, 2), KMP.kmpMatcher("ABCABC", "ABC"));
13+
assertEquals(List.of(0, 3), KMP.kmpMatcher("ABCABC", "ABC"));
1314
assertEquals(List.of(10), KMP.kmpMatcher("ABABDABACDABABCABAB", "ABABCABAB"));
1415
assertEquals(List.of(), KMP.kmpMatcher("ABCDE", "FGH"));
1516
assertEquals(List.of(), KMP.kmpMatcher("A", "AA"));

src/test/java/com/thealgorithms/strings/RabinKarpTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thealgorithms.strings;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
45
import java.util.List;
56
import org.junit.jupiter.api.Test;
67

@@ -9,7 +10,7 @@ public class RabinKarpTest {
910
@Test
1011
public void testRabinKarpSearch() {
1112
assertEquals(List.of(0, 1), RabinKarp.search("AAAAABAAABA", "AAAA"));
12-
assertEquals(List.of(0, 2), RabinKarp.search("ABCABC", "ABC"));
13+
assertEquals(List.of(0, 3), RabinKarp.search("ABCABC", "ABC"));
1314
assertEquals(List.of(10), RabinKarp.search("ABABDABACDABABCABAB", "ABABCABAB"));
1415
assertEquals(List.of(), RabinKarp.search("ABCDE", "FGH"));
1516
assertEquals(List.of(), RabinKarp.search("A", "AA"));

0 commit comments

Comments
 (0)