Pattern matching algorithms
A pattern matching algorithm is used to determine the index positions where a given pattern string (P
) is matched in a text string (T
). Thus, the pattern matching algorithm finds and returns the index where a given string pattern appears in a text string. It returns "pattern not found"
if the pattern does not have a match in the text string.
For example, for the given text string (s) = "packt publisher"
and the pattern string (p) = "publisher"
, the pattern-matching algorithm returns the index position where the pattern string is matched in the text string. An example of a string matching problem is shown in Figure 13.1:
Figure 13.1: An example of a string matching problem
We will discuss four pattern matching algorithms, that is, the brute force method, Rabin-Karp algorithm, and the Knuth-Morris-Pratt (KMP) and Boyer-Moore pattern-matching algorithms. We start with the brute force pattern matching algorithm...