Skip to content

Commit 03ee6f9

Browse files
authored
Merge pull request TheAlgorithms#1323 from prateekKrOraon/master
Addition of Rabin-Karp and String matching using Finite Automata
2 parents 0705384 + de76caa commit 03ee6f9

File tree

2 files changed

+175
-0
lines changed

2 files changed

+175
-0
lines changed

Others/RabinKarp.java

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* @author Prateek Kumar Oraon (https://github.com/prateekKrOraon)
3+
*/
4+
import java.util.Scanner;
5+
import java.lang.Math;
6+
7+
//An implementation of Rabin-Karp string matching algorithm
8+
//Program will simply end if there is no match
9+
public class RabinKarp {
10+
11+
public static Scanner scanner = null;
12+
public final static int d = 256;
13+
14+
public static void main(String[] args){
15+
16+
scanner = new Scanner(System.in);
17+
System.out.println("Enter String");
18+
String text = scanner.nextLine();
19+
System.out.println("Enter pattern");
20+
String pattern = scanner.nextLine();
21+
22+
int q = 101;
23+
searchPat(text,pattern,q);
24+
25+
}
26+
27+
private static void searchPat(String text, String pattern, int q) {
28+
29+
int m = pattern.length();
30+
int n = text.length();
31+
int t = 0;
32+
int p = 0;
33+
int h = 1;
34+
int j = 0;
35+
int i = 0;
36+
37+
h = (int)Math.pow(d,m-1)%q;
38+
39+
for(i =0 ; i< m; i++){
40+
//hash value is calculated for each character and then added with the hash value of the next character for pattern
41+
// as well as the text for length equal to the length of pattern
42+
p = (d*p + pattern.charAt(i))%q;
43+
t = (d*t + text.charAt(i))%q;
44+
}
45+
46+
for(i=0; i<=n-m;i++){
47+
48+
//if the calculated hash value of the pattern and text matches then
49+
//all the characters of the pattern is matched with the text of length equal to length of the pattern
50+
//if all matches then pattern exist in string
51+
//if not then the hash value of the first character of the text is subtracted and hash value of the next character after the end
52+
//of the evaluated characters is added
53+
if(p==t){
54+
55+
//if hash value matches then the individual characters are matched
56+
for(j=0;j<m;j++){
57+
58+
//if not matched then break out of the loop
59+
if(text.charAt(i+j) != pattern.charAt(j))
60+
break;
61+
}
62+
63+
//if all characters are matched then pattern exist in the string
64+
if (j==m){
65+
System.out.println("Pattern found at index " + i);
66+
}
67+
68+
}
69+
70+
//if i<n-m then hash value of the first character of the text is subtracted and hash value of the next character after the end
71+
//of the evaluated characters is added to get the hash value of the next window of characters in the text
72+
if ( i < n-m )
73+
{
74+
t = (d*(t - text.charAt(i)*h) + text.charAt(i+m))%q;
75+
76+
//if hash value becomes less than zero than q is added to make it positive
77+
if (t < 0)
78+
t = (t + q);
79+
}
80+
}
81+
82+
}
83+
84+
}

Others/StringMatchFiniteAutomata.java

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* @author Prateek Kumar Oraon (https://github.com/prateekKrOraon)
3+
*/
4+
import java.util.Scanner;
5+
6+
//An implementaion of string matching using finite automata
7+
public class StringMatchFiniteAutomata{
8+
9+
public static final int CHARS = 256;
10+
public static int[][] FA;
11+
public static Scanner scanner = null;
12+
13+
public static void main(String[] args){
14+
15+
scanner = new Scanner(System.in);
16+
System.out.println("Enter String");
17+
String text = scanner.nextLine();
18+
System.out.println("Enter pattern");
19+
String pat = scanner.nextLine();
20+
21+
searchPat(text, pat);
22+
23+
scanner.close();
24+
25+
}
26+
27+
public static void searchPat(String text, String pat){
28+
29+
int m = pat.length();
30+
int n = text.length();
31+
32+
FA = new int[m+1][CHARS];
33+
34+
computeFA(pat, m ,FA);
35+
36+
int state = 0;
37+
for(int i=0;i<n;i++){
38+
state = FA[state][text.charAt(i)];
39+
40+
if(state == m){
41+
System.out.println("Pattern found at index " + (i-m+1));
42+
}
43+
}
44+
45+
}
46+
47+
//Computes finite automata for the partern
48+
public static void computeFA(String pat, int m, int[][] FA){
49+
50+
for(int state = 0; state<= m; ++state){
51+
for(int x =0; x< CHARS; ++x){
52+
FA[state][x] = getNextState(pat, m, state, x);
53+
}
54+
}
55+
56+
}
57+
58+
public static int getNextState(String pat, int m, int state, int x){
59+
60+
//if current state is less than length of pattern
61+
//and input character of pattern matches the character in the alphabet
62+
//then automata goes to next state
63+
if(state < m && x==pat.charAt(state)){
64+
return state+1;
65+
}
66+
67+
for(int ns = state; ns>0; ns--){
68+
69+
if(pat.charAt(ns-1) == x){
70+
71+
for(int i=0; i<ns-1; i++){
72+
73+
if(pat.charAt(i) != pat.charAt(state-ns+i+1)){
74+
break;
75+
}
76+
77+
if(i == ns-1){
78+
return ns;
79+
}
80+
81+
}
82+
83+
}
84+
85+
}
86+
87+
return 0;
88+
89+
}
90+
91+
}

0 commit comments

Comments
 (0)