Maximum consecutive occurrences of a string in another given string
Given two strings str1 and str2, the task is to count the maximum consecutive occurrences of the string str2 in the string str1.
Examples:
Input: str1 = “abababcba”, str2 = “ba”
Output: 2
Explanation: String str2 occurs consecutively in the substring { str[1], …, str[4] }. Therefore, the maximum count obtained is 2
Input: str1 = “ababc”, str2 = “ac”
Output: 0
Explanation:
Since str2 is not present as a substring in str1, the required output is 0.
Approach: Follow the steps below to solve the problem:
- Initialize a variable, say cntOcc, to store the count of occurrences of str2 in the string str1.
- Iterate over the range [CntOcc, 1]. For every ith value in the iteration, concatenate the string str2 i times and check if the concatenated string is a substring of the string str1 or not. The first ith value for which it is found to be true, print it as the required answer.
Below is the implementation:
C++
#include <bits/stdc++.h>
#include <string.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++;
j = 0;
}
}
return res;
}
int maxRepeating(string str1, string str2)
{
int cntOcc = countFreq(str2, str1);
string Contstr = "" ;
for ( int i = 0; i < cntOcc; i++)
Contstr += str2;
size_t found = str1.find(Contstr);
while (found == string::npos)
{
found = str1.find(Contstr);
cntOcc -= 1;
Contstr = "" ;
for ( int i = 0; i < cntOcc; i++)
Contstr += str2;
}
return cntOcc;
}
int main()
{
string str1 = "abababc" ;
string str2 = "ba" ;
cout << maxRepeating(str1, str2);
return 0;
}
|
Java
import java.util.*;
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 int maxRepeating(String str1, String str2)
{
int cntOcc = countFreq(str2, str1);
String Contstr = "" ;
for ( int i = 0 ; i < cntOcc; i++)
Contstr += str2;
boolean found = str1.contains(Contstr);
while (!found)
{
found = str1.contains(Contstr);
cntOcc -= 1 ;
Contstr = "" ;
for ( int i = 0 ; i < cntOcc; i++)
Contstr += str2;
}
return cntOcc;
}
public static void main(String[] args)
{
String str1 = "abababc" ;
String str2 = "ba" ;
System.out.print(maxRepeating(str1, str2));
}
}
|
Python3
def maxRepeating(str1, str2):
cntOcc = str1.count(str2)
Contstr = str2 * cntOcc
while (Contstr not in str1):
cntOcc - = 1
Contstr = str2 * cntOcc
return cntOcc
if __name__ = = "__main__" :
str1 = "abababc"
str2 = "ba"
print (maxRepeating(str1, str2))
|
C#
using System;
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 int maxRepeating(String str1, String str2)
{
int cntOcc = countFreq(str2, str1);
String Contstr = "" ;
for ( int i = 0; i < cntOcc; i++)
Contstr += str2;
bool found = str1.Contains(Contstr);
while (!found)
{
found = str1.Contains(Contstr);
cntOcc -= 1;
Contstr = "" ;
for ( int i = 0; i < cntOcc; i++)
Contstr += str2;
}
return cntOcc;
}
public static void Main(String[] args)
{
String str1 = "abababc" ;
String str2 = "ba" ;
Console.Write(maxRepeating(str1, str2));
}
}
|
Javascript
<script>
function countFreq(pat, txt) {
var M = pat.length;
var N = txt.length;
var res = 0;
for ( var i = 0; i <= N - M; i++) {
var j;
for (j = 0; j < M; j++)
if (txt[i + j] !== pat[j])
break ;
if (j === M) {
res++;
j = 0;
}
}
return res;
}
function maxRepeating(str1, str2) {
var cntOcc = countFreq(str2, str1);
var Contstr = "" ;
for ( var i = 0; i < cntOcc; i++)
Contstr += str2;
var found = str1.includes(Contstr);
while (!found) {
found = str1.includes(Contstr);
cntOcc -= 1;
Contstr = "" ;
for ( var i = 0; i < cntOcc; i++)
Contstr += str2;
}
return cntOcc;
}
var str1 = "abababc" ;
var str2 = "ba" ;
document.write(maxRepeating(str1, str2));
</script>
|
Time Complexity: O(N2), as we are using nested loops for traversing N*N times.
Auxiliary Space: O(N), as we are using extra space.
Last Updated :
12 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...