Frequency of a substring in a string | Set 2
Last Updated :
21 Mar, 2023
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++
#include <bits/stdc++.h>
using namespace std;
void find_frequency(string S, string pattern)
{
regex c(pattern);
smatch m;
if (regex_search(S, m, c) == true ) {
cout << "Yes"
<< "\n" ;
}
else {
cout << "No" ;
}
ptrdiff_t numberOfMatches = std::distance(
sregex_iterator(S.begin(), S.end(), c),
sregex_iterator());
cout << "Frequency of string " << pattern << " is "
<< numberOfMatches;
}
int32_t main()
{
string str = "geeksforgeeks" ;
string pattern = "geeks" ;
find_frequency(str, pattern);
return 0;
}
|
Java
import java.util.regex.*;
class GFG {
static void find_frequency(String S, String pattern)
{
Pattern c = Pattern.compile(pattern);
Matcher m = c.matcher(S);
if (m.find()) {
System.out.println( "Yes" );
}
else {
System.out.println( "No" );
}
int numberOfMatches = 0 ;
m.reset();
while (m.find()) {
numberOfMatches++;
}
System.out.println( "Frequency of string " + pattern
+ " is " + numberOfMatches);
}
public static void main(String[] args)
{
String str = "geeksforgeeks" ;
String pattern = "geeks" ;
find_frequency(str, pattern);
}
}
|
Python3
import re
def find_frequency(s, pattern):
c = re. compile (pattern)
m = c.search(s)
if m:
print ( "Yes" )
else :
print ( "No" )
numberOfMatches = 0
m = c.finditer(s)
for match in m:
numberOfMatches + = 1
print ( "Frequency of string " + pattern
+ " is " + str (numberOfMatches))
if __name__ = = "__main__" :
s = "geeksforgeeks"
pattern = "geeks"
find_frequency(s, pattern)
|
C#
using System;
using System.Text.RegularExpressions;
class GFG {
static void find_frequency( string S, string pattern)
{
Regex c = new Regex(pattern);
Match m = c.Match(S);
if (m.Success) {
Console.WriteLine( "Yes" );
}
else {
Console.WriteLine( "No" );
}
int numberOfMatches = 0;
m = c.Match(S);
while (m.Success) {
numberOfMatches++;
m = m.NextMatch();
}
Console.WriteLine( "Frequency of string " + pattern
+ " is " + numberOfMatches);
}
public static void Main( string [] args)
{
string str = "geeksforgeeks" ;
string pattern = "geeks" ;
find_frequency(str, pattern);
}
}
|
Javascript
function find_frequency(S, pattern) {
const c = new RegExp(pattern);
const m = S.match(c);
if (m !== null ) {
console.log( "Yes" );
}
else {
console.log( "No" );
}
let numberOfMatches = 0;
let m2 = c.exec(S);
while (m2 != null )
{
numberOfMatches++;
S = S.substr(m2.index + 1);
m2 = c.exec(S);
}
console.log( "Frequency of string " + pattern
+ " is " + numberOfMatches);
}
const str = "geeksforgeeks" ;
const pattern = "geeks" ;
find_frequency(str, pattern);
|
Output:
Yes
Frequency of string geeks is 2
Time Complexity: O(N + M)
Auxiliary Space: O(M)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...