Frequency of a Substring in a String
Last Updated :
18 Apr, 2023
Given an input string and a pattern, the task is to find the frequency of occurrences of the string pattern in a given string.
Examples:
Input: pattern = “man”, string = “dhimanman”
Output: 2
Input: pattern = “nn”, string = “Banana”
Output: 0
Input: pattern = “aa”, string = “aaaaa”
Output : 4
Approach:
A simple solution is to match characters one by one. And whenever we see a complete match, increment count. For this, we can use Naive pattern searching.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int countFreq(string& pat, string& txt)
{
int M = pat.length();
int N = txt.length();
int res = 0;
for ( int i = 0; i <= N - M; i++) {
int j;
for (j = 0; j < M; j++)
if (txt[i + j] != pat[j])
break ;
if (j == M) {
res++;
}
}
return res;
}
int main()
{
string txt = "dhimanman" ;
string pat = "man" ;
cout << countFreq(pat, txt);
return 0;
}
|
Java
class GFG {
static int countFreq(String pat, String txt)
{
int M = pat.length();
int N = txt.length();
int res = 0 ;
for ( int i = 0 ; i <= N - M; i++) {
int j;
for (j = 0 ; j < M; j++) {
if (txt.charAt(i + j) != pat.charAt(j)) {
break ;
}
}
if (j == M) {
res++;
j = 0 ;
}
}
return res;
}
static public void main(String[] args)
{
String txt = "dhimanman" ;
String pat = "man" ;
System.out.println(countFreq(pat, txt));
}
}
|
Python3
def countFreq(pat, txt):
M = len (pat)
N = len (txt)
res = 0
for i in range (N - M + 1 ):
j = 0
while j < M:
if (txt[i + j] ! = pat[j]):
break
j + = 1
if (j = = M):
res + = 1
j = 0
return res
if __name__ = = '__main__' :
txt = "dhimanman"
pat = "man"
print (countFreq(pat, txt))
|
C#
using System;
public class GFG {
static int countFreq(String pat, String txt)
{
int M = pat.Length;
int N = txt.Length;
int res = 0;
for ( int i = 0; i <= N - M; i++) {
int j;
for (j = 0; j < M; j++) {
if (txt[i + j] != pat[j]) {
break ;
}
}
if (j == M) {
res++;
j = 0;
}
}
return res;
}
static public void Main()
{
String txt = "dhimanman" ;
String pat = "man" ;
Console.Write(countFreq(pat, txt));
}
}
|
PHP
<?php
function countFreq( $pat , $txt )
{
$M = strlen ( $pat );
$N = strlen ( $txt );
$res = 0;
for ( $i = 0; $i <= $N - $M ; $i ++)
{
for ( $j = 0; $j < $M ; $j ++)
if ( $txt [ $i + $j ] != $pat [ $j ])
break ;
if ( $j == $M )
{
$res ++;
$j = 0;
}
}
return $res ;
}
$txt = "dhimanman" ;
$pat = "man" ;
echo countFreq( $pat , $txt );
|
Javascript
<script>
let mod = 100000007;
function countFreq(pat, txt)
{
let M = pat.length;
let N = txt.length;
let res = 0;
for (let i = 0; i <= N - M; i++)
{
let j;
for (j = 0; j < M; j++)
{
if (txt[i + j] != pat[j])
{
break ;
}
}
if (j == M)
{
res++;
j = 0;
}
}
return res;
}
let txt = "dhimanman" ;
let pat = "man" ;
document.write(countFreq(pat, txt));
</script>
|
Time Complexity: O(M * N)
Auxiliary Space: O(1)
Efficient Approach:
An efficient solution is to use KMP algorithm.
Below is the implementation of the above approach.
C++
#include <iostream>
using namespace std;
void computeLPSArray(string pat, int M, int lps[])
{
int len = 0;
int i = 1;
lps[0] = 0;
while (i < M) {
if (pat[i] == pat[len]) {
len++;
lps[i] = len;
i++;
}
else
{
if (len != 0) {
len = lps[len - 1];
}
else
{
lps[i] = len;
i++;
}
}
}
}
int KMPSearch(string pat, string txt)
{
int M = pat.length();
int N = txt.length();
int lps[M];
int j = 0;
computeLPSArray(pat, M, lps);
int i = 0;
int res = 0;
int next_i = 0;
while (i < N) {
if (pat[j] == txt[i]) {
j++;
i++;
}
if (j == M) {
j = lps[j - 1];
res++;
}
else if (i < N && pat[j] != txt[i]) {
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
}
}
return res;
}
int main()
{
string txt = "geeksforgeeks" ;
string pat = "eeks" ;
int ans = KMPSearch(pat, txt);
cout << ans;
return 0;
}
|
Java
class KMP_String_Matching {
int KMPSearch(String pat, String txt)
{
int M = pat.length();
int N = txt.length();
int lps[] = new int [M];
int j = 0 ;
computeLPSArray(pat, M, lps);
int i = 0 ;
int res = 0 ;
int next_i = 0 ;
while (i < N) {
if (pat.charAt(j) == txt.charAt(i)) {
j++;
i++;
}
if (j == M) {
j = lps[j - 1 ];
res++;
if (lps[j] != 0 )
i = ++next_i;
j = 0 ;
}
else if (i < N
&& pat.charAt(j) != txt.charAt(i)) {
if (j != 0 )
j = lps[j - 1 ];
else
i = i + 1 ;
}
}
return res;
}
void computeLPSArray(String pat, int M, int lps[])
{
int len = 0 ;
int i = 1 ;
lps[ 0 ] = 0 ;
while (i < M) {
if (pat.charAt(i) == pat.charAt(len)) {
len++;
lps[i] = len;
i++;
}
else
{
if (len != 0 ) {
len = lps[len - 1 ];
}
else
{
lps[i] = len;
i++;
}
}
}
}
public static void main(String args[])
{
String txt = "geeksforgeeks" ;
String pat = "eeks" ;
int ans
= new KMP_String_Matching().KMPSearch(pat, txt);
System.out.println(ans);
}
}
|
Python3
def KMPSearch(pat, txt):
M = len (pat)
N = len (txt)
lps = [ None ] * M
j = 0
computeLPSArray(pat, M, lps)
i = 0
res = 0
next_i = 0
while (i < N):
if pat[j] = = txt[i]:
j = j + 1
i = i + 1
if j = = M:
j = lps[j - 1 ]
res = res + 1
if lps[j] ! = 0 :
next_i = next_i + 1
i = next_i
j = 0
elif ((i < N) and (pat[j] ! = txt[i])):
if (j ! = 0 ):
j = lps[j - 1 ]
else :
i = i + 1
return res
def computeLPSArray(pat, M, lps):
len = 0
i = 1
lps[ 0 ] = 0
while (i < M):
if pat[i] = = pat[ len ]:
len = len + 1
lps[i] = len
i = i + 1
else :
if len ! = 0 :
len = lps[ len - 1 ]
else :
lps[i] = len
i = i + 1
if __name__ = = "__main__" :
txt = "geeksforgeeks"
pat = "eeks"
ans = KMPSearch(pat, txt)
print (ans)
|
C#
using System;
public class KMP_String_Matching {
int KMPSearch(String pat, String txt)
{
int M = pat.Length;
int N = txt.Length;
int [] lps = new int [M];
int j = 0;
computeLPSArray(pat, M, lps);
int i = 0;
int res = 0;
int next_i = 0;
while (i < N) {
if (pat[j] == txt[i]) {
j++;
i++;
}
if (j == M) {
j = lps[j - 1];
res++;
if (lps[j] != 0)
i = ++next_i;
j = 0;
}
else if (i < N && pat[j] != txt[i]) {
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
}
}
return res;
}
void computeLPSArray(String pat, int M, int [] lps)
{
int len = 0;
int i = 1;
lps[0] = 0;
while (i < M) {
if (pat[i] == pat[len]) {
len++;
lps[i] = len;
i++;
}
else
{
if (len != 0) {
len = lps[len - 1];
}
else
{
lps[i] = len;
i++;
}
}
}
}
public static void Main(String[] args)
{
String txt = "geeksforgeeks" ;
String pat = "eeks" ;
int ans
= new KMP_String_Matching().KMPSearch(pat, txt);
Console.WriteLine(ans);
}
}
|
Javascript
<script>
function computeLPSArray(pat,M,lps)
{
let len = 0;
let i = 1;
lps[0] = 0;
while (i < M)
{
if (pat[i] == pat[len])
{
len++;
lps[i] = len;
i++;
}
else
{
if (len != 0)
{
len = lps[len - 1];
}
else
{
lps[i] = len;
i++;
}
}
}
}
function KMPSearch(pat,txt)
{
let M = pat.length;
let N = txt.length;
let lps = new Array(M);
lps.fill(0);
let j = 0;
computeLPSArray(pat, M, lps);
let i = 0;
let res = 0;
let next_i = 0;
while (i < N)
{
if (pat[j] == txt[i])
{
j++;
i++;
}
if (j == M)
{
j = lps[j - 1];
res++;
if (lps[j]!=0)
i = ++next_i;
j = 0;
}
else if (i < N && pat[j] != txt[i])
{
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
}
}
return res;
}
let txt = "geeksforgeeks" ;
let pat = "eeks" ;
let ans = KMPSearch(pat, txt);
document.write(ans);
</script>
|
Time Complexity: O(M + N)
Auxiliary Space: O(M) As an array of size M is used to store the longest prefix suffix values for the pattern.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...