Check if count of substrings in S with string S1 as prefix and S2 as suffix is equal to that with S2 as prefix and S1 as suffix
Given three strings S, S1, and S2, the task is to check if the number of substrings that start and end with S1 and S2 is equal to the number of substrings that start and end with S2 and S1 or not. If found to be true, then print “Yes”. Otherwise, print “No”.
Examples:
Input: S = “helloworldworldhelloworld”, S1 = “hello”, S2 = “world”
Output: No
Explanation:
The substrings that starts with S1 (= “hello”) and ends with S2 (= “world”) are {“helloworld”, “helloworldworld”, “helloworldworldhelloworld”, “helloworld”}.
Therefore, the total count is 4.
The substrings that starts with S2 (= “world”) and ends with S1(= “hello”) are {“worldworldhello”, “worldhello”}.
Therefore, the total count is 2.
Therefore, the above two counts are not the same.
Input: S = “opencloseopencloseopen”, S1 = “open”, S2 = “close”
Output: Yes
Approach: Follow the steps below to solve the problem:
- Store the length of strings S, S1, and S2 in variables, say N, N1, and N2 respectively.
- Initialize a variable, say count, to store the number of occurrences of substring S1 in the string S.
- Initialize a variable, say ans, to store substrings that start and end with S1 and S2 respectively.
- Traverse the given string S and perform the following steps:
- Store the substring of string S starting at the index i of size N1 in the string prefix.
- Store the substring of string S starting at the index i of size N2 in the string suffix.
- If the prefix is the same as the string S1, then increment the value of count by 1.
- If the suffix is the same as the string S2, then increment the value of ans by count.
- Use the above steps, to find the number of substrings that start and end with S2 and S1 respectively. Store the count obtained in the variable say ans2.
- If the values of ans and ans2 are found to be equal, then print “Yes”. Otherwise, print “No”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countSubstrings(string S, string S1,
string S2)
{
int N = S.length();
int N1 = S1.length();
int N2 = S2.length();
int count = 0, totalcount = 0;
for ( int i = 0; i < N; i++) {
string prefix = S.substr(i, N1);
string suffix = S.substr(i, N2);
if (S1 == prefix)
count++;
if (S2 == suffix)
totalcount += count;
}
return totalcount;
}
void checkSubstrings(string S, string S1,
string S2)
{
int x = countSubstrings(S, S1, S2);
int y = countSubstrings(S, S2, S1);
if (x == y)
cout << "Yes" ;
else
cout << "No" ;
}
int main()
{
string S = "opencloseopencloseopen" ;
string S1 = "open" ;
string S2 = "close" ;
checkSubstrings(S, S1, S2);
return 0;
}
|
Java
class GFG{
static int countSubstrings(String S, String S1,
String S2)
{
int N = S.length();
int N1 = S1.length();
int N2 = S2.length();
int count = 0 , totalcount = 0 ;
for ( int i = 0 ; i < N; i++)
{
String prefix = S.substring(
i, (i + N1 < N) ? (i + N1) : N);
String suffix = S.substring(
i, (i + N2 < N) ? (i + N2) : N);
if (S1.equals(prefix))
count++;
if (S2.equals(suffix))
totalcount += count;
}
return totalcount;
}
static void checkSubstrings(String S, String S1,
String S2)
{
int x = countSubstrings(S, S1, S2);
int y = countSubstrings(S, S2, S1);
if (x == y)
System.out.println( "Yes" );
else
System.out.println( "No" );
}
public static void main(String[] args)
{
String S = "opencloseopencloseopen" ;
String S1 = "open" ;
String S2 = "close" ;
checkSubstrings(S, S1, S2);
}
}
|
Python3
def countSubstrings(S, S1, S2):
N = len (S)
N1 = len (S1)
N2 = len (S2)
count = 0
totalcount = 0
for i in range (N):
prefix = S[i: (i + N1) if (i + N1 < N) else N]
suffix = S[i: (i + N2) if (i + N2 < N) else N]
if S1 = = prefix:
count + = 1
if S2 = = suffix:
totalcount + = count
return totalcount
def checkSubstrings(S, S1, S2):
x = countSubstrings(S, S1, S2)
y = countSubstrings(S, S2, S1)
if x = = y:
print ( "Yes" )
else :
print ( "No" )
S = "opencloseopencloseopen"
S1 = "open"
S2 = "close"
checkSubstrings(S, S1, S2)
|
C#
using System;
class GFG{
static int countSubstrings( string S, string S1,
string S2)
{
int N = S.Length;
int N1 = S1.Length;
int N2 = S2.Length;
int count = 0, totalcount = 0;
for ( int i = 0; i < N; i++)
{
String prefix = S.Substring(
i, (i + N1 < N) ? N1 : (N - i));
String suffix = S.Substring(
i, (i + N2 < N) ? N2 : (N - i));
if (S1.Equals(prefix))
count++;
if (S2.Equals(suffix))
totalcount += count;
}
return totalcount;
}
static void checkSubstrings( string S, string S1,
string S2)
{
int x = countSubstrings(S, S1, S2);
int y = countSubstrings(S, S2, S1);
if (x == y)
Console.Write( "Yes" );
else
Console.Write( "No" );
}
static void Main()
{
string S = "opencloseopencloseopen" ;
string S1 = "open" ;
string S2 = "close" ;
checkSubstrings(S, S1, S2);
}
}
|
Javascript
<script>
function countSubstrings(S, S1, S2)
{
let N = S.length;
let N1 = S1.length;
let N2 = S2.length;
let count = 0, totalcount = 0;
for (let i = 0; i < N; i++) {
let prefix = S.substr(i, N1);
let suffix = S.substr(i, N2);
if (S1 == prefix)
count++;
if (S2 == suffix)
totalcount += count;
}
return totalcount;
}
function checkSubstrings(S, S1, S2)
{
let x = countSubstrings(S, S1, S2);
let y = countSubstrings(S, S2, S1);
if (x == y)
document.write( "Yes" );
else
document.write( "No" );
}
let S = "opencloseopencloseopen" ;
let S1 = "open" ;
let S2 = "close" ;
checkSubstrings(S, S1, S2);
</script>
|
Time Complexity: O(N * (N1 + N2)), where N, N1, and N2 are the length of strings S, S1, and S2 respectively.
Auxiliary Space: O(1)
Last Updated :
06 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...