Package edu.princeton.cs.algs4
Class RabinKarp
- Object
-
- edu.princeton.cs.algs4.RabinKarp
-
public class RabinKarp extends Object
TheRabinKarpclass finds the first occurrence of a pattern string in a text string.This implementation uses the Rabin-Karp algorithm.
For additional documentation, see Section 5.3 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static voidmain(String[] args)Takes a pattern string and an input string as command-line arguments; searches for the pattern string in the text string; and prints the first occurrence of the pattern string in the text string.intsearch(String txt)Returns the index of the first occurrence of the pattern string in the text string.
-
-
-
Constructor Detail
-
RabinKarp
public RabinKarp(char[] pattern, int R)Preprocesses the pattern string.- Parameters:
pattern- the pattern stringR- the alphabet size
-
RabinKarp
public RabinKarp(String pat)
Preprocesses the pattern string.- Parameters:
pat- the pattern string
-
-
Method Detail
-
search
public int search(String txt)
Returns the index of the first occurrence of the pattern string in the text string.- Parameters:
txt- the text string- Returns:
- the index of the first occurrence of the pattern string in the text string; n if no such match
-
main
public static void main(String[] args)
Takes a pattern string and an input string as command-line arguments; searches for the pattern string in the text string; and prints the first occurrence of the pattern string in the text string.- Parameters:
args- the command-line arguments
-
-