
- DSA - Home
- DSA - Overview
- DSA - Environment Setup
- DSA - Algorithms Basics
- DSA - Asymptotic Analysis
- Data Structures
- DSA - Data Structure Basics
- DSA - Data Structures and Types
- DSA - Array Data Structure
- DSA - Skip List Data Structure
- Linked Lists
- DSA - Linked List Data Structure
- DSA - Doubly Linked List Data Structure
- DSA - Circular Linked List Data Structure
- Stack & Queue
- DSA - Stack Data Structure
- DSA - Expression Parsing
- DSA - Queue Data Structure
- DSA - Circular Queue Data Structure
- DSA - Priority Queue Data Structure
- DSA - Deque Data Structure
- Searching Algorithms
- DSA - Searching Algorithms
- DSA - Linear Search Algorithm
- DSA - Binary Search Algorithm
- DSA - Interpolation Search
- DSA - Jump Search Algorithm
- DSA - Exponential Search
- DSA - Fibonacci Search
- DSA - Sublist Search
- DSA - Hash Table
- Sorting Algorithms
- DSA - Sorting Algorithms
- DSA - Bubble Sort Algorithm
- DSA - Insertion Sort Algorithm
- DSA - Selection Sort Algorithm
- DSA - Merge Sort Algorithm
- DSA - Shell Sort Algorithm
- DSA - Heap Sort Algorithm
- DSA - Bucket Sort Algorithm
- DSA - Counting Sort Algorithm
- DSA - Radix Sort Algorithm
- DSA - Quick Sort Algorithm
- Matrices Data Structure
- DSA - Matrices Data Structure
- DSA - Lup Decomposition In Matrices
- DSA - Lu Decomposition In Matrices
- Graph Data Structure
- DSA - Graph Data Structure
- DSA - Depth First Traversal
- DSA - Breadth First Traversal
- DSA - Spanning Tree
- DSA - Topological Sorting
- DSA - Strongly Connected Components
- DSA - Biconnected Components
- DSA - Augmenting Path
- DSA - Network Flow Problems
- DSA - Flow Networks In Data Structures
- DSA - Edmonds Blossom Algorithm
- DSA - Maxflow Mincut Theorem
- Tree Data Structure
- DSA - Tree Data Structure
- DSA - Tree Traversal
- DSA - Binary Search Tree
- DSA - AVL Tree
- DSA - Red Black Trees
- DSA - B Trees
- DSA - B+ Trees
- DSA - Splay Trees
- DSA - Range Queries
- DSA - Segment Trees
- DSA - Fenwick Tree
- DSA - Fusion Tree
- DSA - Hashed Array Tree
- DSA - K-Ary Tree
- DSA - Kd Trees
- DSA - Priority Search Tree Data Structure
- Recursion
- DSA - Recursion Algorithms
- DSA - Tower of Hanoi Using Recursion
- DSA - Fibonacci Series Using Recursion
- Divide and Conquer
- DSA - Divide and Conquer
- DSA - Max-Min Problem
- DSA - Strassen's Matrix Multiplication
- DSA - Karatsuba Algorithm
- Greedy Algorithms
- DSA - Greedy Algorithms
- DSA - Travelling Salesman Problem (Greedy Approach)
- DSA - Prim's Minimal Spanning Tree
- DSA - Kruskal's Minimal Spanning Tree
- DSA - Dijkstra's Shortest Path Algorithm
- DSA - Map Colouring Algorithm
- DSA - Fractional Knapsack Problem
- DSA - Job Sequencing with Deadline
- DSA - Optimal Merge Pattern Algorithm
- Dynamic Programming
- DSA - Dynamic Programming
- DSA - Matrix Chain Multiplication
- DSA - Floyd Warshall Algorithm
- DSA - 0-1 Knapsack Problem
- DSA - Longest Common Sub-sequence Algorithm
- DSA - Travelling Salesman Problem (Dynamic Approach)
- Hashing
- DSA - Hashing Data Structure
- DSA - Collision In Hashing
- Disjoint Set
- DSA - Disjoint Set
- DSA - Path Compression And Union By Rank
- Heap
- DSA - Heap Data Structure
- DSA - Binary Heap
- DSA - Binomial Heap
- DSA - Fibonacci Heap
- Tries Data Structure
- DSA - Tries
- DSA - Standard Tries
- DSA - Compressed Tries
- DSA - Suffix Tries
- Treaps
- DSA - Treaps Data Structure
- Bit Mask
- DSA - Bit Mask In Data Structures
- Bloom Filter
- DSA - Bloom Filter Data Structure
- Approximation Algorithms
- DSA - Approximation Algorithms
- DSA - Vertex Cover Algorithm
- DSA - Set Cover Problem
- DSA - Travelling Salesman Problem (Approximation Approach)
- Randomized Algorithms
- DSA - Randomized Algorithms
- DSA - Randomized Quick Sort Algorithm
- DSA - Karger’s Minimum Cut Algorithm
- DSA - Fisher-Yates Shuffle Algorithm
- Miscellaneous
- DSA - Infix to Postfix
- DSA - Bellmon Ford Shortest Path
- DSA - Maximum Bipartite Matching
- DSA Useful Resources
- DSA - Questions and Answers
- DSA - Selection Sort Interview Questions
- DSA - Merge Sort Interview Questions
- DSA - Insertion Sort Interview Questions
- DSA - Heap Sort Interview Questions
- DSA - Bubble Sort Interview Questions
- DSA - Bucket Sort Interview Questions
- DSA - Radix Sort Interview Questions
- DSA - Cycle Sort Interview Questions
- DSA - Quick Guide
- DSA - Useful Resources
- DSA - Discussion
DSA - Pattern Searching
What is Pattern Searching?
The pattern searching/matching algorithm is a technique that is used to locate or find a specific pattern or substring within given text. Its basic idea is to find all the occurrences of a particular pattern in the specified data structure. For instance, given an array of numbers [1, 2, 3, 4, 5, 6, 3, 4, 9] and we need to find all the occurrences of the pattern [3, 4] in the array. The answer would be index number 2 and 6.

How pattern searching algorithm works?
There are multiple pattern searching algorithms that works in different ways. The main goal to design these type of algorithms is to reduce the time complexity. The traditional approach may take lots of time to complete the pattern searching task for a longer text.
If we are saying traditional approach, we are actually referring to brute force approach for solving pattern searching problems. Let's see how it works −
Slide the pattern over the text.
Compare each character one by one.
If all the characters match, we have found a match.
If not, move the pattern one position to the right and repeat the process until we reach the end of the text or find a match.

Remember, brute force is the simplest way to solve string matching or searching, but it is also the slowest and most inefficient. The time complexity of this algorithm is O(nm), where 'n' is the length of the text and 'm' is the length of the pattern. This means that in the worst case, we have to compare every character of the text with every character of the pattern, which can be very slow if n and m are large. There are other algorithms also that are mentioned in the next section.
Example
In this example, we will practically demonstrates the brute force approach to solve a pattern matching problem in various programming languages.
#include <stdio.h> #include <string.h> // method to find match void findMatch(char *orgText, char *pattern) { int n = strlen(orgText); int m = strlen(pattern); // comparing the text and pattern one by one for (int i = 0; i <= n-m; i++) { int j = 0; while (j < m && orgText[i+j] == pattern[j]) { j++; } // print result if found if (j == m) { printf("Oohhoo! Match found at index: %d\n", i); return; } } // if not found printf("Oopps! No match found\n"); } int main() { // Original Text char orgText[] = "Tutorials Point"; // pattern to be matched char pattern[] = "Point"; // method calling findMatch(orgText, pattern); return 0; }
#include <iostream> #include <string> using namespace std; // method to find match void findMatch(string orgText, string pattern) { int n = orgText.length(); int m = pattern.length(); // comparing the text and pattern one by one for (int i = 0; i <= n-m; i++) { int j = 0; while (j < m && orgText[i+j] == pattern[j]) { j++; } // print result if found if (j == m) { cout << "Oohhoo! Match found at index: " << i << endl; return; } } // if not found cout << "Oopps! No match found" << endl; } int main() { // Original Text string orgText = "Tutorials Point"; // pattern to be matched string pattern = "Point"; // method calling findMatch(orgText, pattern); return 0; }
public class PattrnMatch { public static void main(String[] args) { // Original Text String orgText = "Tutorials Point"; // pattern to be matched String pattern = "Point"; // method calling findMatch(orgText, pattern); } // method to find match public static void findMatch(String orgText, String pattern) { int n = orgText.length(); int m = pattern.length(); // comparing the text and pattern one by one for (int i = 0; i <= n-m; i++) { int j = 0; while (j < m && orgText.charAt(i+j) == pattern.charAt(j)) { j++; } // print result if found if (j == m) { System.out.println("Oohhoo! Match found at index: " + i); return; } } // if not found System.out.println("Oopps! No match found"); } }
# method to find match def findMatch(orgText, pattern): n = len(orgText) m = len(pattern) # comparing the text and pattern one by one for i in range(n-m+1): j = 0 while j < m and orgText[i+j] == pattern[j]: j += 1 # print result if found if j == m: print("Oohhoo! Match found at index:", i) return # if not found print("Oopps! No match found") # Original Text orgText = "Tutorials Point" # pattern to be matched pattern = "Point" # method calling findMatch(orgText, pattern)
Output
Oohhoo! Match found at index: 10
Pattern Searching Algorithms in Data Structure
Various pattern searching techniques can be applied on the data structures to retrieve certain patterns. A pattern searching operation is said to be successful only if it returns the desired element or its index, otherwise, the operation considered as unsuccessful.
Here is the list of pattern searching algorithms that we are going to cover in this tutorial −
- Naive Pattern Searching Algorithm
- Knuth-Morris-Pratt Algorithm
- Boyer Moore Algorithm
- Efficient Construction of Finite Automata Algorithm
- Aho-Corasick Algorithm
- Suffix Array Algorithm
- Kasai's Algorithm
- Manacher's Algorithm
- Rabin-Karp Algorithm
- Z Algorithm
Application of Pattern Searching Algorithms
The applications of pattern-searching algorithms are as follows −
- Bioinformatics − It is a field that applies pattern searching algorithms to analyse biological data, such as DNA and protein structure.
- Text processing − Text processing involves tasks of finding a string from a collection of documents. It is essential for plagiarism detection, spell and grammar checking, search engine queries, etc.
- Data Security − Pattern matching algorithms can be used in data security by establishing malware detection and password identification systems.
- Sentiment analysis − By matching or detecting the tone of words, we can analyse the accent, emotion and sentiment of a user.
- Recommendation system − We can also analyse the content of a video, audio or any blog through pattern matching algorithms which further help in recommending other content.