11package com .thealgorithms .strings ;
22
3- import java .util .Scanner ;
3+ import java .util .ArrayList ;
4+ import java .util .List ;
45
56/**
67 * @author Prateek Kumar Oraon (https://github.com/prateekKrOraon)
78 *
8- An implementation of Rabin-Karp string matching algorithm
9- Program will simply end if there is no match
9+ * An implementation of Rabin-Karp string matching algorithm
10+ * Program will simply end if there is no match
1011 */
1112public final class RabinKarp {
1213 private RabinKarp () {
1314 }
1415
15- public static Scanner scanner = null ;
16- public static final int ALPHABET_SIZE = 256 ;
16+ private static final int ALPHABET_SIZE = 256 ;
1717
18- public static void main (String [] args ) {
19- scanner = new Scanner (System .in );
20- System .out .println ("Enter String" );
21- String text = scanner .nextLine ();
22- System .out .println ("Enter pattern" );
23- String pattern = scanner .nextLine ();
24-
25- int q = 101 ;
26- searchPat (text , pattern , q );
18+ public static List <Integer > search (String text , String pattern ) {
19+ return search (text , pattern , 101 );
2720 }
2821
29- private static void searchPat (String text , String pattern , int q ) {
22+ public static List <Integer > search (String text , String pattern , int q ) {
23+ List <Integer > occurrences = new ArrayList <>();
24+ if (text == null || pattern == null || pattern .isEmpty ()) {
25+ return occurrences ;
26+ }
27+
3028 int m = pattern .length ();
3129 int n = text .length ();
3230 int t = 0 ;
@@ -35,48 +33,42 @@ private static void searchPat(String text, String pattern, int q) {
3533 int j = 0 ;
3634 int i = 0 ;
3735
38- h = (int ) Math .pow (ALPHABET_SIZE , m - 1 ) % q ;
36+ if (m > n ) {
37+ return new ArrayList <>();
38+ }
39+
40+ // h = pow(ALPHABET_SIZE, m-1) % q
41+ for (i = 0 ; i < m - 1 ; i ++) {
42+ h = h * ALPHABET_SIZE % q ;
43+ }
3944
4045 for (i = 0 ; i < m ; i ++) {
41- // hash value is calculated for each character and then added with the hash value of the
42- // next character for pattern as well as the text for length equal to the length of
43- // pattern
4446 p = (ALPHABET_SIZE * p + pattern .charAt (i )) % q ;
4547 t = (ALPHABET_SIZE * t + text .charAt (i )) % q ;
4648 }
4749
4850 for (i = 0 ; i <= n - m ; i ++) {
49- // if the calculated hash value of the pattern and text matches then
50- // all the characters of the pattern is matched with the text of length equal to length
51- // of the pattern if all matches then pattern exist in string if not then the hash value
52- // of the first character of the text is subtracted and hash value of the next character
53- // after the end of the evaluated characters is added
5451 if (p == t ) {
55- // if hash value matches then the individual characters are matched
5652 for (j = 0 ; j < m ; j ++) {
57- // if not matched then break out of the loop
5853 if (text .charAt (i + j ) != pattern .charAt (j )) {
5954 break ;
6055 }
6156 }
6257
63- // if all characters are matched then pattern exist in the string
6458 if (j == m ) {
65- System . out . println ( "Pattern found at index " + i );
59+ occurrences . add ( i );
6660 }
6761 }
6862
69- // if i<n-m then hash value of the first character of the text is subtracted and hash
70- // value of the next character after the end of the evaluated characters is added to get
71- // the hash value of the next window of characters in the text
7263 if (i < n - m ) {
73- t = (ALPHABET_SIZE * (t - text .charAt (i ) * h ) + text .charAt (i + m )) % q ;
74-
75- // if hash value becomes less than zero than q is added to make it positive
64+ t = (t - text .charAt (i ) * h % q );
7665 if (t < 0 ) {
77- t = ( t + q ) ;
66+ t += q ;
7867 }
68+ t = t * ALPHABET_SIZE % q ;
69+ t = (t + text .charAt (i + m )) % q ;
7970 }
8071 }
72+ return occurrences ;
8173 }
8274}
0 commit comments