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”.
Input: str = “geeksforgeeks”, pattern = “geeks”
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”
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 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)
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:
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.
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Frequency of a substring in a string using pthread
- Substring of length K having maximum frequency in the given string
- Maximum length substring with highest frequency in a string
- Frequency of a substring in a string
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Generate a number such that the frequency of each digit is digit times the frequency in given number
- Count of Binary Strings of length N such that frequency of 1's exceeds frequency of 0's
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Substring with highest frequency length product
- Minimum length of substring whose rotation generates a palindromic substring
- Find length of longest subsequence of one string which is substring of another string
- Minimum changes required to make first string substring of second string
- Check if given string is a substring of string formed by repeated concatenation of z to a
- Smallest String consisting of a String S exactly K times as a Substring
- Lexicographic smallest permutation of a String containing the second String as a Substring
- Lengths of maximized partitions of a string such that each character of the string appears in one substring
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.