-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRabin_karp.java
More file actions
71 lines (60 loc) · 2.11 KB
/
Rabin_karp.java
File metadata and controls
71 lines (60 loc) · 2.11 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
package Algorithms;
public class Rabin_karp
{
//number of characters in the input alphabet
public final static int d = 256;
void RKsearch(String pat, String txt, int q)
{
int M = pat.length();
int N = txt.length();
int i, j,flag=0;
int p = 0; // hash value for pattern
int t = 0; // hash value for txt
int h = 1;
for (i = 0; i < M-1; i++)
h = (h*d)%q;
// Calculate the hash value of pattern and first window of text
for (i = 0; i < M; i++)
{
p = (d*p + pat.charAt(i))%q;
t = (d*t + txt.charAt(i))%q;
}
// checking for pattern
for (i = 0; i <= N - M; i++)
{
// Check the hash values of current window of text
// and pattern. If the hash values match then only
// check for characters on by one
if (p == t)
{
//checking each character
for (j = 0; j < M; j++)
{
if (txt.charAt(i+j) != pat.charAt(j))
break;
}
// if pattern exists in text
if (j == M) {
flag=1;
System.out.println("Pattern found at index " + i); }
}
if ( i < N-M )
{
t = (d*(t - txt.charAt(i)*h) + txt.charAt(i+M))%q; //calculating hash value
//making t positive
if (t < 0)
t = (t + q);
}
}
if(flag==0)
System.out.println("Pattern not found");
}
/* Driver program to test above function
public static void main(String[] args)
{
String txt = "Hello world";
String pat = "ell";
int q = 101; // A prime number
//RKsearch(pat, txt, q);
} */
}