-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path9thJune.java
More file actions
80 lines (61 loc) · 2.37 KB
/
Copy path9thJune.java
File metadata and controls
80 lines (61 loc) · 2.37 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
75
76
77
78
79
80
public class RobinKrap_Pattern {
public static void main(String[] args) {
/*Rabin karp
formula = T(at i+1) = (T(at i)-txt[i]*d^(p-1))*d - txt[i+p]
where , p = pattern length
T = hash code of string
1. Slide the pattern one by one.
2. Do the hash
3. If hash are same then compare the character.
4. repeat step 1 if again still string exhauxst
// TC = O(n+m) --> avg+best
// TC = O(mn) --> worst
*/
// String str = "AXAYABCDAXABZZAWABAD";
// String pattern = "AB";
String str = "batmanandrobinarebat";
String pattern = "bat";
int n = str.length();
int p = pattern.length();
int d = p; // d is pattern length... can be any number... (our choice)
int q = 13; // any prime number ... (our choice)
int x = 1;
int stringHash = 0;
int patternHash = 0;
for(int i =0; i<p-1; i++){
x=(x*d)%q; // for finding d^(m-1)
}
for(int i = 0; i<p;i++){
patternHash = (patternHash * d + (pattern.charAt(i)))%q;
stringHash = (stringHash * d + (str.charAt(i)))%q;
}
// now sliding window logic
for(int i =0; i<=n-p; i++){
if(stringHash==patternHash){ // for checking spurius hit
int j;
for(j=0; j<p;j++){
if(pattern.charAt(j)!=str.charAt(i+j)){
break;
}
}
if(j==p){
System.out.println("Pattern match: "+i);
}
}
// recompute str
// updation of window ... by deleting former and adding latter
if(i<n-p){
// // removing old string hash of slide
// stringHash=stringHash-(str.charAt(i)*x);
// // add new char hash of the slide
// stringHash=(stringHash*d+str.charAt(i+p))%q;
stringHash=((stringHash-(str.charAt(i)*x))*d+str.charAt(i+p))%q;
// T(at i+1) = ((T(at i)-txt[i]*d^(p-1))*d + txt[i+p])%q
//base case if ..string Hash will become less than 0
if(stringHash<0){
stringHash=stringHash+q;
}
}
}
}
}