#### Overview of this book

Choosing the right data structure is pivotal to optimizing the performance and scalability of applications. This new edition of Hands-On Data Structures and Algorithms with Python will expand your understanding of key structures, including stacks, queues, and lists, and also show you how to apply priority queues and heaps in applications. You’ll learn how to analyze and compare Python algorithms, and understand which algorithms should be used for a problem based on running time and computational complexity. You will also become confident organizing your code in a manageable, consistent, and scalable way, which will boost your productivity as a Python developer. By the end of this Python book, you’ll be able to manipulate the most important data structures and algorithms to more efficiently store, organize, and access data in your applications.
Preface
Free Chapter
Python Data Types and Structures
Introduction to Algorithm Design
Algorithm Design Techniques and Strategies
Stacks and Queues
Trees
Heaps and Priority Queues
Hash Tables
Graphs and Algorithms
Searching
Sorting
Selection Algorithms
String Matching Algorithms
Other Books You May Enjoy
Index

# 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...