Maximum number of overlapping string
Last Updated :
10 Feb, 2023
Given two strings S and T, the task is to count the number of the overlapping string T in the string S.
Note: If there are some mismatched words as a subsequence that do not match the string T, then print -1.
Examples:
Input: S = “chirpchirp”, T = “chirp”
Output: 0
Explanation:
There are no overlapping strings of “chirp”.
Input: S = “chcirphirp”, T = “chirp”
Output: 2
There are two overlapping string of T:
First “chirp” can be chcirphirp.
Second “chirp” can be chcirphirp.
Approach: The idea is to iterate over the string S and increase the overlapping count at an instant when the first character of the string T occurs If at any instant the current character is equal to the last character then decrement the overlapping count. Meanwhile, update the maximum overlapping count if it is greater than 2. Finally, to check that every subsequence matches to the string T check overlapping count is equal to zero or not. If yes return the maximum overlap count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxOverlap(string S, string T)
{
string str = T;
int count[T.length()] = { 0 };
int overlap = 0;
int max_overlap = 0;
for ( int i = 0; i <= S.length(); i++) {
int index = str.find(S[i]);
if (index == 0) {
overlap++;
if (overlap >= 2)
max_overlap = max(overlap, max_overlap);
count[index]++;
}
else {
if (count[index - 1] <= 0)
return -1;
count[index]++;
count[index - 1]--;
}
if (index == 4)
overlap--;
}
if (overlap == 0)
return max_overlap;
else
return -1;
}
int main()
{
string S = "chcirphirp" ;
string T = "chirp" ;
cout << maxOverlap(S, T);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int maxOverlap(String S, String T)
{
String str = T;
int count[] = new int [T.length()];
int overlap = 0 ;
int max_overlap = 0 ;
for ( int i = 0 ; i < S.length(); i++)
{
int index = str.indexOf(S.charAt(i));
if (index == 0 )
{
overlap++;
if (overlap >= 2 )
max_overlap = Math.max(overlap,
max_overlap);
count[index]++;
}
else
{
if (count[index - 1 ] <= 0 )
return - 1 ;
count[index]++;
count[index - 1 ]--;
}
if (index == 4 )
overlap--;
}
if (overlap == 0 )
return max_overlap;
else
return - 1 ;
}
public static void main(String[] args)
{
String S = "chcirphirp" ;
String T = "chirp" ;
System.out.print(maxOverlap(S, T));
}
}
|
Python3
def maxOverlap(S, T):
str = T
count = [ 0 for i in range ( len (T))]
overlap = 0
max_overlap = 0
for i in range ( 0 , len (S)):
index = str .find(S[i])
if (index = = 0 ):
overlap + = 1
if (overlap > = 2 ):
max_overlap = max (overlap,
max_overlap)
count[index] + = 1
else :
if (count[index - 1 ] < = 0 ):
return - 1
count[index] + = 1
count[index - 1 ] - = 1
if (index = = 4 ):
overlap - = 1
if (overlap = = 0 ):
return max_overlap
else :
return - 1
S = "chcirphirp"
T = "chirp"
print (maxOverlap(S, T))
|
C#
using System;
class GFG{
static int maxOverlap(String S, String T)
{
String str = T;
int []count = new int [T.Length];
int overlap = 0;
int max_overlap = 0;
for ( int i = 0; i < S.Length; i++)
{
int index = str.IndexOf(S[i]);
if (index == 0)
{
overlap++;
if (overlap >= 2)
{
max_overlap = Math.Max(overlap,
max_overlap);
}
count[index]++;
}
else
{
if (count[index - 1] <= 0)
return -1;
count[index]++;
count[index - 1]--;
}
if (index == 4)
overlap--;
}
if (overlap == 0)
return max_overlap;
else
return -1;
}
public static void Main(String[] args)
{
String S = "chcirphirp" ;
String T = "chirp" ;
Console.Write(maxOverlap(S, T));
}
}
|
Javascript
const maxOverlap = (S, T) => {
let str = T;
let count = Array(T.length).fill(0);
let overlap = 0;
let max_overlap = 0;
for (let i = 0; i < S.length; i++)
{
let index = str.indexOf(S[i]);
if (index === 0) {
overlap++;
if (overlap >= 2) max_overlap = Math.max(overlap, max_overlap);
count[index]++;
} else {
if (count[index - 1] <= 0) return -1;
count[index]++;
count[index - 1]--;
}
if (index === 4) overlap--;
}
if (overlap === 0) return max_overlap;
else return -1;
};
const S = "chcirphirp" ;
const T = "chirp" ;
console.log(maxOverlap(S, T));
|
Time Complexity: O(n * m), where n and m are the length of the given strings S and T respectively.
Auxiliary Space: O(m)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...