Skip to content

Create a new Rabin-Karp Algorithm #3201

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 5, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update and rename RabinKarpSearch to RabinKarpAlgorithm.java
  • Loading branch information
tackhwa authored Aug 3, 2022
commit e6769c95f413847d82a933bd99518eeb986e751a
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ public class RabinKarpAlgorithm
txt -> text
q -> A prime number
*/
static void search(String pat, String txt, int q)
public int search(String pat, String txt, int q)
{
int index = -1; //note: -1 here represent not found, it is not an index
int M = pat.length();
int N = txt.length();
int i, j;
Expand Down Expand Up @@ -49,8 +50,11 @@ static void search(String pat, String txt, int q)
}

// if p == t and pat[0...M-1] = txt[i, i+1, ...i+M-1]
if (j == M)
if (j == M) {
System.out.println("Pattern found at index " + i);
index= i;
return index ;
}
}

// Calculate hash value for next window of text: Remove
Expand All @@ -65,20 +69,9 @@ static void search(String pat, String txt, int q)
t = (t + q);
}
}
return index; // return -1 if pattern does not found
}

/* Driver Code */
public static void main(String[] args)
{
String txt = "This is an example for rabin karp algorithmn";
String pat = "for";

// A prime number
int q = 101;

// Function Call
search(pat, txt, q);
}
}

// This code is contributed by nuclode