-
Notifications
You must be signed in to change notification settings - Fork 21k
Expand file tree
/
Copy pathRabinKarp.java
More file actions
74 lines (62 loc) · 1.91 KB
/
RabinKarp.java
File metadata and controls
74 lines (62 loc) · 1.91 KB
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
69
70
71
72
73
74
package com.thealgorithms.strings;
import java.util.ArrayList;
import java.util.List;
/**
* @author Prateek Kumar Oraon (https://github.com/prateekKrOraon)
*
* An implementation of Rabin-Karp string matching algorithm
* Program will simply end if there is no match
*/
public final class RabinKarp {
private RabinKarp() {
}
private static final int ALPHABET_SIZE = 256;
public static List<Integer> search(String text, String pattern) {
return search(text, pattern, 101);
}
public static List<Integer> search(String text, String pattern, int q) {
List<Integer> occurrences = new ArrayList<>();
if (text == null || pattern == null || pattern.isEmpty()) {
return occurrences;
}
int m = pattern.length();
int n = text.length();
int t = 0;
int p = 0;
int h = 1;
int j = 0;
int i = 0;
if (m > n) {
return new ArrayList<>();
}
// h = pow(ALPHABET_SIZE, m-1) % q
for (i = 0; i < m - 1; i++) {
h = h * ALPHABET_SIZE % q;
}
for (i = 0; i < m; i++) {
p = (ALPHABET_SIZE * p + pattern.charAt(i)) % q;
t = (ALPHABET_SIZE * t + text.charAt(i)) % q;
}
for (i = 0; i <= n - m; i++) {
if (p == t) {
for (j = 0; j < m; j++) {
if (text.charAt(i + j) != pattern.charAt(j)) {
break;
}
}
if (j == m) {
occurrences.add(i);
}
}
if (i < n - m) {
t = (t - text.charAt(i) * h % q);
if (t < 0) {
t += q;
}
t = t * ALPHABET_SIZE % q;
t = (t + text.charAt(i + m)) % q;
}
}
return occurrences;
}
}