
- 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
Naive Pattern Searching Algorithm
Naive Pattern Searching Algorithm in Data Structure
Naive pattern searching is the simplest method among other pattern searching algorithms. Although, it is more efficient than the brute force approach, however, it is not the most optimal method available. Like brute force, it also checks for all characters of the main string in order to find the pattern. Hence, its time complexity is O(m*n) where the 'm' is the size of pattern and 'n' is the size of the main string. This algorithm is helpful for smaller texts only.
The naive pattern searching algorithm does not require any pre-processing phases. We can find substring by checking once for the string. It also does not occupy extra space to perform the operation. If a match is found, the end result of the pattern matching operation will be the index of specified pattern, otherwise -1. Furthermore, this operation can return all the indices if the desired pattern appears multiple times within the main string.
Let's understand the input-output scenario of a pattern matching problem with an example −
Input: main String: "ABAAABCDBBABCDDEBCABC" pattern: "ABC" Output: Pattern found at position: 4 Pattern found at position: 10 Pattern found at position: 18

Example
In the following example, we are going to demonstrate how to apply a naive approach to solve a pattern matching problem.
#include<stdio.h> #include<string.h> // method to search for pattern void naiveFindPatrn(char* mainString, char* pattern, int array[], int *index) { int patLen = strlen(pattern); int strLen = strlen(mainString); // outer for loop for(int i = 0; i<=(strLen - patLen); i++) { int j; // to check for each character of pattern for(j = 0; j<patLen; j++) { if(mainString[i+j] != pattern[j]) break; } // to print the index of the pattern is found if(j == patLen) { (*index)++; array[(*index)] = i; } } } // main method starts int main() { // main string char mainString[] = "ABAAABCDBBABCDDEBCABC"; // pattern to be found char pattern[] = "ABC"; int locArray[strlen(mainString)]; int index = -1; naiveFindPatrn(mainString, pattern, locArray, &index); // to print the indices for(int i = 0; i <= index; i++) { printf("Pattern found at position: %d\n", locArray[i]); } return 0; }
#include<iostream> using namespace std; // method to search for pattern void naiveFindPatrn(string mainString, string pattern, int array[], int *index) { int patLen = pattern.size(); int strLen = mainString.size(); // outer for loop for(int i = 0; i<=(strLen - patLen); i++) { int j; // to check for each character of pattern for(j = 0; j<patLen; j++) { if(mainString[i+j] != pattern[j]) break; } // to print the index of the pattern is found if(j == patLen) { (*index)++; array[(*index)] = i; } } } // main method starts int main() { // main string string mainString = "ABAAABCDBBABCDDEBCABC"; // pattern to be found string pattern = "ABC"; int locArray[mainString.size()]; int index = -1; naiveFindPatrn(mainString, pattern, locArray, &index); // to print the indices for(int i = 0; i <= index; i++) { cout << "Pattern found at position: " << locArray[i]<<endl; } }
public class Main { // method to search for pattern static void naiveFindPatrn(String mainString, String pattern, int[] array) { int patLen = pattern.length(); int strLen = mainString.length(); int index = 0; // outer for loop for(int i = 0; i <= (strLen - patLen); i++) { int j; // to check for each character of pattern for(j = 0; j < patLen; j++) { if(mainString.charAt(i+j) != pattern.charAt(j)) break; } // to print the index of the pattern is found if(j == patLen) { array[index] = i; index++; } } } // main method starts public static void main(String[] args) { // main string String mainString = "ABAAABCDBBABCDDEBCABC"; // pattern to be found String pattern = "ABC"; int[] locArray = new int[mainString.length()]; naiveFindPatrn(mainString, pattern, locArray); // to print the indices for(int i = 0; i < locArray.length && locArray[i] != 0; i++) { System.out.println("Pattern found at position: " + locArray[i]); } } }
# method to search for pattern def naiveFindPatrn(mainString, pattern): patLen = len(pattern) strLen = len(mainString) indices = [] # outer for loop for i in range(strLen - patLen + 1): j = 0 # to check for each character of pattern for j in range(patLen): if mainString[i+j] != pattern[j]: break # to print the index of the pattern is found if j == patLen - 1 and mainString[i+j] == pattern[j]: indices.append(i) return indices # main method starts if __name__ == "__main__": # main string mainString = "ABAAABCDBBABCDDEBCABC" # pattern to be found pattern = "ABC" indices = naiveFindPatrn(mainString, pattern) # to print the indices for i in indices: print("Pattern found at position:", i)
Output
Pattern found at position: 4 Pattern found at position: 10 Pattern found at position: 18