The Wayback Machine - https://web.archive.org/web/20210517012724/https://www.geeksforgeeks.org/count-occurrences-of-substring-x-before-every-occurrence-of-substring-y-in-a-given-string/
Skip to content
Related Articles

Related Articles

Count occurrences of substring X before every occurrence of substring Y in a given string
  • Last Updated : 16 May, 2021

Given three strings S, X, and Y consisting of N, A, and B characters respectively, the task is to find the number of occurrences of the substring X before every occurrence of the substring Y in the given string S.

Examples:

Input S = ”abcdefdefabc”, X = ”def”, Y = ”abc”
Output: 0 2
Explanation:
First occurence of Y(= “abc”): No of occurrences of X(= “def”) = 0.
Second occurrence of Y: No of occurrences of X = 0.

Input: S = ”accccbbbbbbaaa”, X = ”a”, Y = ”b”
Output: 0 6 6 6

Approach: Follow the steps below to solve the problem:



  • Initialize a variable, say count, that stores the total number occurrences of X.
  • Traverse the given string S and perform the following steps:
    • If the substring over the range [i, B] is equal to Y, then increment count by 1.
    • If the substring over the range [i, A] is equal to X, then print the value of count as the resultant count of the string Y before the current occurrence of the string X.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to count occurrences of
// the string Y in the string S for
// every occurrence of X in S
void countOccurrences(string S,
                      string X,
                      string Y)
{
    // Stores the count of
    // occurrences of X
    int count = 0;
  
    // Stores the lengths of the
    // three strings
    int N = S.length(), A = X.length();
    int B = Y.length();
  
    // Traverse the string S
    for (int i = 0; i < N; i++) {
  
        // If the current substring
        // is Y, then increment the
        // value of count by 1
        if (S.substr(i, B) == Y)
            count++;
  
        // If the current substring
        // is X, then print the count
        if (S.substr(i, A) == X)
            cout << count << " ";
    }
}
  
// Driver Code
int main()
{
    string S = "abcdefdefabc";
    string X = "abc";
    string Y = "def";
    countOccurrences(S, X, Y);
  
    return 0;
}
Output:
0 2

Time Complexity: O(N*(A + B))
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :