Package dsa

Class KMP


  • public class KMP
    extends Object
    A data type to find the first occurrence of a pattern string within a text string, using the Knuth-Morris-Pratt (KMP) substring search algorithm.
    • Constructor Detail

      • KMP

        public KMP​(String pattern,
                   int radix)
        Preprocesses the pattern string with alphabet size given by radix.
        Parameters:
        pattern - the pattern string.
        radix - the alphabet size.
    • Method Detail

      • search

        public int search​(String text)
        Returns the index of the first occurrence of the pattern string within the text string, or the length of the text string.
        Parameters:
        text - the text string.
        Returns:
        the index of the first occurrence of the pattern string within the text string, or the length of the text string.
      • main

        public static void main​(String[] args)
        Unit tests the data type.
        Parameters:
        args - the command-line arguments.