Count pairs of substrings from a string S such that S1 does not occur after S2 in each pair
Last Updated :
04 Oct, 2021
Given three strings str, str1 and str2, the task is to count the number of pairs of occurrences of str1 and str2 as a substring in the string str such that in each pair, the starting index of str1 is less than or equal to str2.
Examples:
Input: str = “geeksforgeeksfor”, str1 = “geeks”, str2 = “for”
Output: 3
Explanation:
Starting indices of str1 are { 0, 8 }
Starting indices of str2 are { 5, 13 }
Possible pairs such that start index of str1 less than or equal to str2 are { (0, 5), (0, 13), (8, 13) }
Therefore, the required output is 3.
Input: str = “geeksforgeeks”, str1 = “geek”, str2 = “for”
Output: 1
Approach: The idea is to find the starting indices of the substring str2 in the string str and at every starting index of string str2 increment the count of pairs by the count of starting indices of str1 up to the ith index of str. Finally, print the total count of pairs obtained. Follow the steps below to solve the problem:
- Initialize a variable, say cntPairs, to store the count of pairs of str1 and str2 such that the starting index of str1 is less than or equal to str2.
- Initialize a variable, say cntStr1, to store the count of substring, str1 in str whose start index is less than or equal to i.
- Iterate over the range [0, N – 1]. For every ith index of the string str check if the substring str1 exists in the string str whose starting index is equal to i or not. If found to be true, then increment the value of cntStr1 by 1.
- For every ith index, check if the substring str2 is present in the string whose starting index is equal to i or not. If found to be true, then increment the value of cntPairs by cntStr1.
- Finally, print the value of cntPairs.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int CountSubstring(string str, string str1, string str2)
{
int cntPairs = 0;
int cntStr1 = 0;
for ( int i = 0; i < str.length(); i++) {
if (str.substr(i, str1.length())
== str1) {
cntStr1++;
}
else if (str.substr(i, str2.length())
== str2) {
cntPairs += cntStr1;
}
}
return cntPairs;
}
int main()
{
string str = "geeksforgeeksfor" ;
string str1 = "geeks" , str2 = "for" ;
cout << CountSubstring(str, str1, str2);
return 0;
}
|
Java
public class GFG {
public static void main(String[] args)
{
String str = new String( "geeksforgeeksfor" );
String str1 = new String( "geeks" );
String str2 = new String( "for" );
System.out.println(CountSubstring(
str, str1, str2));
}
static int CountSubstring(String str,
String str1,
String str2)
{
int cntPairs = 0 ;
int cntStr1 = 0 ;
int N = str.length();
int s1 = str1.length();
int s2 = str2.length();
for ( int i = 0 ; i < N; i++) {
if (((i + s1) <= N)
&& (str.substring(
i, (i + s1)))
.compareTo(str1)
== 0 ) {
cntStr1++;
}
else if (((i + s2) <= N)
&& (str.substring(
i, (i + s2)))
.compareTo(str2)
== 0 ) {
cntPairs += cntStr1;
}
}
return cntPairs;
}
}
|
Python3
def CountSubstring( str , str1, str2):
cntStr1 = 0
cntPairs = 0
for i in range ( len ( str )):
if str [i : i + len (str1)] = = str1:
cntStr1 + = 1
elif str [i : i + len (str2)] = = str2:
cntPairs + = cntStr1
return cntPairs
str = 'geeksforgeeksfor'
str1 = 'geeks'
str2 = 'for'
print (CountSubstring( str , str1, str2))
|
C#
using System;
class GFG{
static int CountSubstring(String str,
String str1,
String str2)
{
int cntPairs = 0;
int cntStr1 = 0;
int N = str.Length;
int s1 = str1.Length;
int s2 = str2.Length;
for ( int i = 0; i < N; i++)
{
if (((i + s1) <= N) &&
(str.Substring(i, s1)).Equals(str1))
{
cntStr1++;
}
else if (((i + s2) <= N) &&
(str.Substring(i, s2)).Equals(str2))
{
cntPairs += cntStr1;
}
}
return cntPairs;
}
static public void Main()
{
String str = "geeksforgeeksfor" ;
String str1 = "geeks" ;
String str2 = "for" ;
Console.Write(CountSubstring(str, str1, str2));
}
}
|
Javascript
<script>
function CountSubstring(str, str1, str2)
{
var cntPairs = 0;
var cntStr1 = 0;
for (let i = 0; i < str.length; i++) {
if (str.substr(i, str1.length)
== str1) {
cntStr1++;
}
else if (str.substr(i, str2.length)
== str2) {
cntPairs += cntStr1;
}
}
return cntPairs;
}
var str = "geeksforgeeksfor" ;
var str1 = "geeks" , str2 = "for" ;
console.log( CountSubstring(str, str1, str2));
</script>
|
Time Complexity: O(|str| * (|str1| + |str2|))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...