
- Data Structures & Algorithms
- DSA - Home
- DSA - Overview
- DSA - Environment Setup
- Algorithm
- DSA - Algorithms Basics
- DSA - Asymptotic Analysis
- DSA - Greedy Algorithms
- DSA - Divide and Conquer
- DSA - Dynamic Programming
- Data Structures
- DSA - Data Structure Basics
- DSA - Data Structures and Types
- DSA - Array Data Structure
- Linked Lists
- DSA - Linked List Basics
- DSA - Doubly Linked List
- DSA - Circular Linked List
- Stack & Queue
- DSA - Stack
- DSA - Expression Parsing
- DSA - Queue
- Searching Techniques
- DSA - Linear Search
- DSA - Binary Search
- DSA - Interpolation Search
- DSA - Hash Table
- Sorting Techniques
- DSA - Sorting Algorithms
- DSA - Bubble Sort
- DSA - Insertion Sort
- DSA - Selection Sort
- DSA - Merge Sort
- DSA - Shell Sort
- DSA - Quick Sort
- Graph Data Structure
- DSA - Graph Data Structure
- DSA - Depth First Traversal
- DSA - Breadth First Traversal
- 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 - Spanning Tree
- DSA - Tries
- DSA - Heap
- Recursion
- DSA - Recursion Basics
- DSA - Tower of Hanoi
- DSA - Fibonacci Series
- DSA Useful Resources
- DSA - Questions and Answers
- DSA - Quick Guide
- DSA - Useful Resources
- DSA - Discussion
Naive Pattern Searching
Naïve pattern searching is the simplest method among other pattern searching algorithms. It checks for all character of the main string to the pattern. This algorithm is helpful for smaller texts. It does not need 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.
The time complexity of Naïve Pattern Search method is O(m*n). The m is the size of pattern and n is the size of the main string.
Input and Output
Input: Main String: “ABAAABCDBBABCDDEBCABC”, pattern: “ABC” Output: Pattern found at position: 4 Pattern found at position: 10 Pattern found at position: 18
Algorithm
naivePatternSearch(pattern, text)
Input − The text and the pattern
Output − location, where patterns are present in the text
Begin patLen := pattern Size strLen := string size for i := 0 to (strLen - patLen), do for j := 0 to patLen, do if text[i+j] ≠ pattern[j], then break the loop done if j == patLen, then display the position i, as there pattern found done End
Example
#include<iostream> using namespace std; void naivePatternSearch(string mainString, string pattern, int array[], int *index) { int patLen = pattern.size(); int strLen = mainString.size(); for(int i = 0; i<=(strLen - patLen); i++) { int j; for(j = 0; j<patLen; j++) { //check for each character of pattern if it is matched if(mainString[i+j] != pattern[j]) break; } if(j == patLen) { //the pattern is found (*index)++; array[(*index)] = i; } } } int main() { string mainString = "ABAAABCDBBABCDDEBCABC"; string pattern = "ABC"; int locArray[mainString.size()]; int index = -1; naivePatternSearch(mainString, pattern, locArray, &index); for(int i = 0; i <= index; i++) { cout << "Pattern found at position: " << locArray[i]<<endl; } }
Output
Pattern found at position: 4 Pattern found at position: 10 Pattern found at position: 18
- Related Articles
- C Program for Naive algorithm for Pattern Searching
- Introduction to Pattern Searching Algorithms
- Aho-Corasick Algorithm for Pattern Searching in C++
- C Program for KMP Algorithm for Pattern Searching
- C++ program for Finite Automata algorithm for Pattern Searching
- C Program for Rabin-Karp Algorithm for Pattern Searching
- Z algorithm (Linear time pattern searching Algorithm) in C++
- Program for Rabin-Karp Algorithm for Pattern Searching in C
- Improving Naive Bayes Algorithm for Spam Detection
- What are the characteristics of Naive Bayes Classifiers?
- Naive Bayes algorithm: Prior, likelihood and marginal likelihood
- Introduction to Searching Algorithms
- File Searching using C#
- File Searching using Python
- Python Pandas - Convert naive Timestamp to local time zone

Advertisements