Skip to content
Related Articles

Related Articles

Improve Article

Frequency of a substring in a string | Set 2

  • Difficulty Level : Medium
  • Last Updated : 30 Oct, 2020

Given a string str of length N and a substring pattern of length M, the task is to find the frequency of occurrences of pattern as a substring in the given string. If pattern is present in the string str, then print “Yes” with the count of its occurrence. Otherwise, print “No”.

Examples:

Input: str = “geeksforgeeks”, pattern = “geeks”
Output: 2
Explanation:
The occurrence of the string “geeks” in the string “geeksforgeeks” is at index 0 and 8.
Therefore, the count is 2.

Input: str = “dhimanman”, pattern = “max”
Output: 0

Naive Approach: Refer to the previous post for the simplest approach to solve the problem.
Time Complexity: O(N*M)
Auxiliary Space: O(1)

Approach using KMP Algorithm: Refer to the previous post of this article to solve the problem using KMP algorithm.
Time Complexity: O(N + M)
Auxiliary Space: O(M)

Approach using Regular Expression: Follow the steps below to solve the problem:



  • Form the regular expression of the string pattern using regex() function.
  • Create a smatch M using function smatch().
  • Check the presence of the string pattern in the string str using function regex_match() as:

regex_search(str, m, c)
where,
str is the given string,
m is smatch,
c is the regular expression of the string pattern.

  • In the above steps, if the function regex_match() returns True, then print “Yes” and find the occurrence of the string pattern. Otherwise, print “No”.
  • Create a variable numberOfMatches of data type ptrdiff_t to store the count of occurrence.
  • Find the numberOfMatches using function regex_iterator() function as:

ptrdiff_t numberOfMatches = std::distance(sregex_iterator(S.begin(), S.end(), c), sregex_iterator())

  • Print the count of occurrence in the above steps as the result.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the frequency of
// substring in the given string S
void find_frequency(string S,
                    string pattern)
{
    // Create a regular expression
    // of the string pattern
    regex c(pattern);
  
    // Determines the matching behavior
    smatch m;
  
    // Use member function on 'm'
    // regex_search to check if
    // string X is present in S or not
    if (regex_search(S, m, c) == true) {
        cout << "Yes"
             << "\n";
    }
    else {
        cout << "No";
    }
  
    // Count the number of matches
    ptrdiff_t numberOfMatches
        = std::distance(
            sregex_iterator(S.begin(),
                            S.end(), c),
            sregex_iterator());
  
    // Print the coun of occurrence
    cout << "Frequency of string "
         << pattern << " is "
         << numberOfMatches;
}
// Driver code
int32_t main()
{
    // Given string str and pattern
    string str = "geeksforgeeks";
    string pattern = "geeks";
  
    // Function Call
    find_frequency(str, pattern);
  
    return 0;
}
Output:
Yes
Frequency of string geeks is 2

Time Complexity: O(N + M)
Auxiliary Space: O(M)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :