Open In App

Count pairs of substrings from a string S such that S1 does not occur after S2 in each pair

Last Updated : 04 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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:
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++




// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the count of pairs of str1
// and str2 in str such that starting index of
// str1 less than or equal to str2
int CountSubstring(string str, string str1, string str2)
{
 
    // Stores count of pairs of str1 and str2
    // such that the start index of str1 is
    // less than or equal to str2
    int cntPairs = 0;
 
    // Stores count of substring str1 in
    // str whose start index is up to i
    int cntStr1 = 0;
 
    // Iterate over the range [0, N -1]
    for (int i = 0; i < str.length(); i++) {
 
        // If substring str1 whose
        // start index is i
        if (str.substr(i, str1.length())
            == str1) {
 
            // Update cntStr1
            cntStr1++;
        }
 
        // If substring str2 whose
        // start index is i
        else if (str.substr(i, str2.length())
                 == str2) {
 
            // Update cntPairs
            cntPairs += cntStr1;
        }
    }
 
    return cntPairs;
}
 
// Driver Code
int main()
{
 
    string str = "geeksforgeeksfor";
    string str1 = "geeks", str2 = "for";
 
    cout << CountSubstring(str, str1, str2);
 
    return 0;
}


Java




// Java program to implement
// the above approach
public class GFG {
 
    // Driver Code
    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));
    }
 
    // Function to find the count of pairs of str1
    // and str2 in str such that starting index of
    // str1 less than or equal to str2
    static int CountSubstring(String str,
                              String str1,
                              String str2)
    {
 
        // Stores count of pairs of str1 and str2
        // such that the start index of str1 is
        // less than or equal to str2
        int cntPairs = 0;
 
        // Stores count of substring str1 in
        // str whose start index is up to i
        int cntStr1 = 0;
 
        // Stores length of str
        int N = str.length();
 
        // Stores length of str1
        int s1 = str1.length();
 
        // Stores length of str2
        int s2 = str2.length();
 
        // Iterate over the range [0, N -1]
        for (int i = 0; i < N; i++) {
 
            // If substring str1 whose
            // starting index is i
            if (((i + s1) <= N)
                && (str.substring(
                        i, (i + s1)))
                           .compareTo(str1)
                       == 0) {
 
                // Update cntStr1
                cntStr1++;
            }
 
            // If substring str2 whose
            // starting index is i
            else if (((i + s2) <= N)
                     && (str.substring(
                             i, (i + s2)))
                                .compareTo(str2)
                            == 0) {
 
                // Update cntPairs
                cntPairs += cntStr1;
            }
        }
 
        return cntPairs;
    }
}


Python3




# Python3 program to implement
# the above approach
 
# Function to find the count of pairs of str1
# and str2 in str such that starting index of
# str1 less than or equal to str2
def CountSubstring(str, str1, str2):
     
    # Stores count of pairs of str1 and str2
    # such that the start index of str1 is
    # less than or equal to str2
    cntStr1 = 0
     
    # Stores count of substring str1 in
    # str whose start index is up to i
    cntPairs = 0
     
    # Iterate over the range [0, N -1]
    for i in range(len(str)):
         
        # If substring str1 whose
        # start index is i
        if str[i : i + len(str1)] == str1:
             
            # Update cntStr1
            cntStr1 += 1
             
        # If substring str2 whose
        # start index is i   
        elif str[i : i + len(str2)] == str2:
             
            # Update cntPairs
            cntPairs += cntStr1
     
    return cntPairs
     
# Driver Code
str = 'geeksforgeeksfor'
str1 = 'geeks'
str2 = 'for'       
        
print(CountSubstring(str, str1, str2))       
 
# This code is contributed by dharanendralv23


C#




// C# program to implement
// the above approach
using System;
 
class GFG{
   
// Function to find the count of pairs of str1
// and str2 in str such that starting index of
// str1 less than or equal to str2 
static int CountSubstring(String str,
                          String str1,
                          String str2)
{
     
    // Stores count of pairs of str1 and str2
    // such that the start index of str1 is
    // less than or equal to str2
    int cntPairs = 0;
 
    // Stores count of substring str1 in
    // str whose start index is up to i
    int cntStr1 = 0;
 
    // Stores length of str
    int N = str.Length;
 
    // Stores length of str1
    int s1 = str1.Length;
 
    // Stores length of str2
    int s2 = str2.Length;
 
    // Iterate over the range [0, N -1]
    for(int i = 0; i < N; i++)
    {
         
        // If substring str1 whose
        // starting index is i
        if (((i + s1) <= N) &&
            (str.Substring(i, s1)).Equals(str1))
        {
             
            // Update cntStr1
            cntStr1++;
        }
 
        // If substring str2 whose
        // starting index is i
        else if (((i + s2) <= N) &&
                 (str.Substring(i, s2)).Equals(str2))
        {
             
            // Update cntPairs
            cntPairs += cntStr1;
        }
    }
    return cntPairs;
}
 
// Driver code
static public void Main()
{
    String str = "geeksforgeeksfor";        
    String str1 = "geeks";   
    String str2 = "for";
     
    Console.Write(CountSubstring(str, str1, str2));        
}
}
 
// This code is contributed by dharanendralv23


Javascript




<script>
// JavaScript program to implement
// the above approach
 
// Function to find the count of pairs of str1
// and str2 in str such that starting index of
// str1 less than or equal to str2
function CountSubstring(str, str1, str2)
{
 
    // Stores count of pairs of str1 and str2
    // such that the start index of str1 is
    // less than or equal to str2
    var cntPairs = 0;
 
    // Stores count of substring str1 in
    // str whose start index is up to i
    var cntStr1 = 0;
 
    // Iterate over the range [0, N -1]
    for (let i = 0; i < str.length; i++) {
 
        // If substring str1 whose
        // start index is i
        if (str.substr(i, str1.length)
            == str1) {
 
            // Update cntStr1
            cntStr1++;
        }
 
        // If substring str2 whose
        // start index is i
        else if (str.substr(i, str2.length)
                 == str2) {
 
            // Update cntPairs
            cntPairs += cntStr1;
        }
    }
 
    return cntPairs;
}
 
// Driver Code
 
var str = "geeksforgeeksfor";
var str1 = "geeks", str2 = "for";
 
console.log( CountSubstring(str, str1, str2));
 
// This code is contributed by ukasp. 
</script>


Output: 

3

 

Time Complexity: O(|str| * (|str1| + |str2|)) 
Auxiliary Space: O(1)

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads