Open In App

Pattern Searching using C++ library

Last Updated : 03 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a text txt[0..n-1] and a pattern pat[0..m-1], write a function that prints all occurrences of pat[] in txt[]. You may assume that n > m.
Examples: 

Input : txt[] = "geeks for geeks"
        pat[] = "geeks"
Output : Pattern found at index 0
         Pattern found at index 10

Input : txt[] = "aaaa"
        pat[] = "aa"
Output : Pattern found at index 0
         Pattern found at index 1
         Pattern found at index 2

The idea is to use find() in C++ string class.

CPP




// CPP program to print all occurrences of a pattern
// in a text
#include <bits/stdc++.h>
using namespace std;
 
void printOccurrences(string txt, string pat)
{
    int found = txt.find(pat);
    while (found != string::npos) {
        cout << "Pattern found at index " << found << endl;
        found = txt.find(pat, found + 1);
    }
}
 
int main()
{
    string txt = "aaaa", pat = "aa";
    printOccurrences(txt, pat);
    return 0;
}


Output: 

Pattern found at index 0
Pattern found at index 1
Pattern found at index 2

 

Time and Space Complexity:

The given program uses the string find() function to find all occurrences of a pattern in a text. The find() function returns the index of the first occurrence of the pattern in the text, or string::npos if the pattern is not found. The program repeatedly calls the find() function with an updated starting index until all occurrences of the pattern are found.

The time complexity of the program is determined by the time complexity of the find() function, which is O(NM), where N is the length of the text and M is the length of the pattern. In the worst case, the program may need to call the find() function for every substring of length M in the text, resulting in a time complexity of O(NM^2).

The space complexity of the program is determined by the space used to store the text and pattern strings. In addition, the program uses some local variables for indexing and temporary storage, but their space usage is negligible compared to the input strings. Therefore, the space complexity of the program is O(N+M), where N and M are the lengths of the text and pattern strings, respectively.

Overall, the program is efficient for finding all occurrences of a pattern in a small to medium-sized text. However, for large texts, the time complexity of the program may become a bottleneck, and more advanced string matching algorithms, such as the Knuth-Morris-Pratt algorithm or the Boyer-Moore algorithm, may be more appropriate.


Previous Article
Next Article

Similar Reads

Pattern Searching using a Trie of all Suffixes
Problem Statement: Given a text txt[0..n-1] and a pattern pat[0..m-1], write a function search(char pat[], char txt[]) that prints all occurrences of pat[] in txt[]. You may assume that n &gt; m.As discussed in the previous post, we discussed that there are two ways efficiently solve the above problem.1) Preprocess Pattern: KMP Algorithm, Rabin Kar
13 min read
Pattern Searching using Suffix Tree
Given a text txt[0..n-1] and a pattern pat[0..m-1], write a function search(char pat[], char txt[]) that prints all occurrences of pat[] in txt[]. You may assume that n &gt; m. Preprocess Pattern or Preprocess Text? We have discussed the following algorithms in the previous posts: KMP Algorithm Rabin Karp Algorithm Finite Automata based Algorithm B
4 min read
Real time optimized KMP Algorithm for Pattern Searching
In the article, we have already discussed the KMP algorithm for pattern searching. In this article, a real-time optimized KMP algorithm is discussed. From the previous article, it is known that KMP(a.k.a. Knuth-Morris-Pratt) algorithm preprocesses the pattern P and constructs a failure function F(also called as lps[]) to store the length of the lon
7 min read
Rabin-Karp algorithm for Pattern Searching in Matrix
Given matrices txt[][] of dimensions m1 x m2 and pattern pat[][] of dimensions n1 x n2, the task is to check whether a pattern exists in the matrix or not, and if yes then print the top most indices of the pat[][] in txt[][]. It is assumed that m1, m2 ? n1, n2 Examples: Input: txt[][] = {{G, H, I, P} {J, K, L, Q} {R, G, H, I} {S, J, K, L} } pat[][]
15+ min read
KMP Algorithm for Pattern Searching
Given a text txt[0 . . . N-1] and a pattern pat[0 . . . M-1], write a function search(char pat[], char txt[]) that prints all occurrences of pat[] in txt[]. You may assume that N &gt; M. Examples: Input: txt[] = "THIS IS A TEST TEXT", pat[] = "TEST"Output: Pattern found at index 10 Input: txt[] = "AABAACAADAABAABA" pat[] = "AABA"Output: Pattern fou
15+ min read
Rabin-Karp Algorithm for Pattern Searching
Given a text T[0. . .n-1] and a pattern P[0. . .m-1], write a function search(char P[], char T[]) that prints all occurrences of P[] present in T[] using Rabin Karp algorithm. You may assume that n &gt; m. Examples: Input: T[] = "THIS IS A TEST TEXT", P[] = "TEST"Output: Pattern found at index 10 Input: T[] = "AABAACAADAABAABA", P[] = "AABA"Output:
15 min read
Optimized Algorithm for Pattern Searching
Question: We have discussed the Naive String matching algorithm here. Consider a situation where all characters of a pattern are different. Can we modify the original Naive String Matching algorithm so that it works better for these types of patterns? If we can, then what are the changes to the original algorithm? Solution: In the original Naive St
7 min read
Finite Automata algorithm for Pattern Searching
Given a text txt[0..n-1] and a pattern pat[0..m-1], write a function search(char pat[], char txt[]) that prints all occurrences of pat[] in txt[]. You may assume that n &gt; m.Examples: Input: txt[] = "THIS IS A TEST TEXT" pat[] = "TEST" Output: Pattern found at index 10 Input: txt[] = "AABAACAADAABAABA" pat[] = "AABA" Output: Pattern found at inde
13 min read
Pattern Searching | Set 6 (Efficient Construction of Finite Automata)
In the previous post, we discussed the Finite Automata-based pattern searching algorithm. The FA (Finite Automata) construction method discussed in the previous post takes O((m^3)*NO_OF_CHARS) time. FA can be constructed in O(m*NO_OF_CHARS) time. In this post, we will discuss the O(m*NO_OF_CHARS) algorithm for FA construction. The idea is similar t
9 min read
Z algorithm (Linear time pattern searching Algorithm)
This algorithm finds all occurrences of a pattern in a text in linear time. Let length of text be n and of pattern be m, then total time taken is O(m + n) with linear space complexity. Now we can see that both time and space complexity is same as KMP algorithm but this algorithm is Simpler to understand.In this algorithm, we construct a Z array. Wh
13 min read