-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatternMatchUsingHashing.java
More file actions
59 lines (46 loc) · 1.79 KB
/
PatternMatchUsingHashing.java
File metadata and controls
59 lines (46 loc) · 1.79 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
import java.util.*;
public class PatternMatchUsingHashing {
public static List<Integer> rabinKarp(String text, String pattern) {
List<Integer> result = new ArrayList<>();
int base = 256; // Number of characters in the input alphabet
int prime = 101; // A prime number
int n = text.length();
int m = pattern.length();
if (m > n) return result;
int h = 1;
for (int i = 0; i < m - 1; i++) {
h = (h * base) % prime;
}
int pHash = 0; // Hash value for pattern
int tHash = 0; // Hash value for text window
// Initial hash computation
for (int i = 0; i < m; i++) {
pHash = (base * pHash + pattern.charAt(i)) % prime;
tHash = (base * tHash + text.charAt(i)) % prime;
}
// Slide the pattern over text one by one
for (int i = 0; i <= n - m; i++) {
// If the hash values match, check the actual substring
if (pHash == tHash) {
if (text.substring(i, i + m).equals(pattern)) {
result.add(i);
}
}
// Calculate hash for next window
if (i < n - m) {
tHash = (base * (tHash - text.charAt(i) * h) + text.charAt(i + m)) % prime;
// Make sure hash is positive
if (tHash < 0) {
tHash += prime;
}
}
}
return result;
}
public static void main(String[] args) {
String text = "thisresearchworkisunique";
String pattern = "search";
List<Integer> result = rabinKarp(text, pattern);
System.out.println(result); // Output: [6]
}
}