Count occurrences of substring X before every occurrence of substring Y in a given string


In this problem, we need to count the total occurrences of the substring X in the str when we find the substring Y in the given string. We can keep counting the occurrences of the substring X, and when we get the substring Y, we can print the count value.

Problem statement – We have given a string str, X, and Y. The length of the strings is N, A, and B, respectively. We need to total the total number of substring X in the given string str before every occurrence of the substring Y.

Sample examples

Inputstr = "stuxystuxy"; X = "stu"; Y = "xy";

Output– 1 2

Explanation

We have 1 occurrence of substring X (stuxy) before the first occurrence of substring Y

We have 2 occurrences of substring X (stuxystuxy) before the second occurrence of substring Y.

Input– str = ‘aaccddee’ X = ‘a’ Y = ‘e’

Output– 2 2

Explanation– Before the first and second ‘e’, the ‘a’ occurs 2 times.

Input– str = ‘xyz’ X = ‘pq’ Y = ‘yz’

Output– 0

Explanation– The substring ‘yz’ only occurs once in the given string, but ‘pq’ doesn’t occur. So, the output is 0.

Approach 1

In this approach, If we find the substring X in the given string, we will increase the value of the count by 1. When we get the substring Y in the string, we will print the value of the count to show the number of occurrences of substring X before every occurrence of substring Y.

Algorithm

  • Define the ‘cntx’ variable and initialize it with zero.

  • Store the length of the str, X, and Y string into the len, A, and B variables.

  • Use the for loop to traverse the string.

  • If we find substring X starting from the current index, increase the value of ‘cntx’ by 1.

  • If we find substring Y starting from the current index, print the value of ‘cntx’.

Example

#include <bits/stdc++.h>
using namespace std;

// function to cntX the occurrences of substring X before every occurrence of substring Y in the given string
void countOccurrences(string str, string X, string Y) {
   // to store occurrences of X
   int cntX = 0;
   // get the lengths of strings str, X and Y
   int len = str.length();
   int A = X.length();
   int B = Y.length();
   // Traverse the string str
   for (int i = 0; i < len; i++) {
      // If the substring str[i, i+A-1] equals X, then increment cntX by 1.
      if (str.substr(i, A) == X)
          cntX++;
      // If the substring str[i, i+B-1] is equal to Y, then print cntX
      if (str.substr(i, B) == Y)
          cout << cntX << " ";
   }
}

int main() {
   string str = "stuxystuxy";
   string X = "stu";
   string Y = "xy";
   countOccurrences(str, X, Y);
   return 0;
}

Output

1 2

Time complexity – O(len*(A + B)), when len is the length of the string str. The A and B is the length of the substring X and Y, respectively.

Space complexity – O(1) as we use constant space to count the occurrence of substring X.

In the above code, we print the value of ‘cntx’ when we find substring Y starting from the ith index. Programmers can create an array and store the value of ‘cntX’ in the array when we find substring Y. After that. They can return the resultant array from the function as in real-time development, it is required to return the result from the function rather than printing.

Updated on: 18-Aug-2023

88 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements