Skip to content

Commit 92bd9ba

Browse files
authored
Add Rabin-Karp String Search Algorithm (TheAlgorithms#3201)
1 parent 965c203 commit 92bd9ba

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.thealgorithms.searches;
2+
// Following program is a Java implementation
3+
// of Rabin Karp Algorithm given in the CLRS book
4+
5+
public class RabinKarpAlgorithm
6+
{
7+
// d is the number of characters in the input alphabet
8+
public final static int d = 256;
9+
10+
/* pat -> pattern
11+
txt -> text
12+
q -> A prime number
13+
*/
14+
public int search(String pat, String txt, int q)
15+
{
16+
int index = -1; //note: -1 here represent not found, it is not an index
17+
int M = pat.length();
18+
int N = txt.length();
19+
int i, j;
20+
int p = 0; // hash value for pattern
21+
int t = 0; // hash value for txt
22+
int h = 1;
23+
24+
// The value of h would be "pow(d, M-1)%q"
25+
for (i = 0; i < M-1; i++)
26+
h = (h*d)%q;
27+
28+
// Calculate the hash value of pattern and first
29+
// window of text
30+
for (i = 0; i < M; i++)
31+
{
32+
p = (d*p + pat.charAt(i))%q;
33+
t = (d*t + txt.charAt(i))%q;
34+
}
35+
36+
// Slide the pattern over text one by one
37+
for (i = 0; i <= N - M; i++)
38+
{
39+
40+
// Check the hash values of current window of text
41+
// and pattern. If the hash values match then only
42+
// check for characters one by one
43+
if ( p == t )
44+
{
45+
/* Check for characters one by one */
46+
for (j = 0; j < M; j++)
47+
{
48+
if (txt.charAt(i+j) != pat.charAt(j))
49+
break;
50+
}
51+
52+
// if p == t and pat[0...M-1] = txt[i, i+1, ...i+M-1]
53+
if (j == M) {
54+
System.out.println("Pattern found at index " + i);
55+
index= i;
56+
return index ;
57+
}
58+
}
59+
60+
// Calculate hash value for next window of text: Remove
61+
// leading digit, add trailing digit
62+
if ( i < N-M )
63+
{
64+
t = (d*(t - txt.charAt(i)*h) + txt.charAt(i+M))%q;
65+
66+
// We might get negative value of t, converting it
67+
// to positive
68+
if (t < 0)
69+
t = (t + q);
70+
}
71+
}
72+
return index; // return -1 if pattern does not found
73+
}
74+
75+
}
76+
77+
// This code is contributed by nuclode
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.thealgorithms.searches;
2+
3+
import org.junit.jupiter.api.Test;
4+
import static org.junit.jupiter.api.Assertions.*;
5+
6+
7+
class RabinKarpAlgorithmTest {
8+
9+
10+
RabinKarpAlgorithm RKA= new RabinKarpAlgorithm();
11+
int q= 101;
12+
13+
@Test
14+
// valid test case
15+
public void RabinKarpAlgorithmTestExample() {
16+
String txt = "This is an example for rabin karp algorithmn";
17+
String pat = "algorithmn";
18+
int value = RKA.search(pat, txt, q);
19+
assertEquals(value,34);
20+
}
21+
22+
@Test
23+
// valid test case
24+
public void RabinKarpAlgorithmTestFront() {
25+
String txt = "AAABBDDG";
26+
String pat = "AAA";
27+
int value = RKA.search(pat, txt, q);
28+
assertEquals(value, 0);
29+
}
30+
31+
@Test
32+
// valid test case
33+
public void RabinKarpAlgorithmTestMiddle() {
34+
String txt = "AAABBCCBB";
35+
String pat = "BBCC";
36+
int value = RKA.search(pat, txt, q);
37+
assertEquals(value, 3);
38+
}
39+
40+
@Test
41+
// valid test case
42+
public void RabinKarpAlgorithmTestLast() {
43+
String txt = "AAAABBBBCCC";
44+
String pat = "CCC";
45+
int value = RKA.search(pat, txt, q);
46+
assertEquals(value, 8);
47+
}
48+
49+
@Test
50+
// valid test case
51+
public void RabinKarpAlgorithmTestNotFound() {
52+
String txt = "ABCBCBCAAB";
53+
String pat = "AADB";
54+
int value = RKA.search(pat, txt, q);
55+
assertEquals(value, -1);
56+
}
57+
58+
}

0 commit comments

Comments
 (0)