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 ;
@@ -38,45 +36,30 @@ private static void searchPat(String text, String pattern, int q) {
3836 h = (int ) Math .pow (ALPHABET_SIZE , m - 1 ) % q ;
3937
4038 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
4439 p = (ALPHABET_SIZE * p + pattern .charAt (i )) % q ;
4540 t = (ALPHABET_SIZE * t + text .charAt (i )) % q ;
4641 }
4742
4843 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
5444 if (p == t ) {
55- // if hash value matches then the individual characters are matched
5645 for (j = 0 ; j < m ; j ++) {
57- // if not matched then break out of the loop
5846 if (text .charAt (i + j ) != pattern .charAt (j )) {
5947 break ;
6048 }
6149 }
6250
63- // if all characters are matched then pattern exist in the string
6451 if (j == m ) {
65- System . out . println ( "Pattern found at index " + i );
52+ occurrences . add ( i );
6653 }
6754 }
6855
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
7256 if (i < n - m ) {
7357 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
7658 if (t < 0 ) {
7759 t = (t + q );
7860 }
7961 }
8062 }
63+ return occurrences ;
8164 }
8265}
0 commit comments