Given two strings S and T of length N and M respectively, the task is to count the number of substrings of S that contains the string T in it as a substring.
Examples:
Input: S = “dabc”, T = “ab”
Output: 4
Explanation:
Substrings of S containing T as a substring are:
- S[0, 2] = “dab”
- S[1, 2] = “ab”
- S[1, 3] = “abc”
- S[0, 3] = “dabc”
Input: S = “hshshshs” T = “hs”
Output: 25
Naive Approach: For the simplest approach to solve the problem, refer to the previous post of this article.
Time Complexity: O(N2)
Auxiliary Space: O(N2)
Efficient Approach: To optimize the above approach, the idea is to find out all the occurrences of T in S. Whenever T is found in S, add all the substrings which contain this occurrence of T excluding the substrings which were already calculated in the previous occurrences. Follow the steps below to solve the problem:
- Initialize a variable, say answer, to store the count of substrings.
- Initialize a variable, say last, to store the starting index of the last occurrence of T in S.
- Iterate over the range [0, N – M] using a variable, say i.
- Check if the substring S[i, i + M] is equal to T or not. If found to be true, then add (i + 1 – last) * (N – (i + M – 1)) to answer and update last to (i + 1).
- Otherwise, continue for the next iteration.
- After completing the above steps, print the value of the answer as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findOccurrences(string S, string T)
{
int n1 = S.size();
int n2 = T.size();
int ans = 0;
int last = 0;
for ( int i = 0; i <= n1 - n2; i++) {
bool chk = true ;
for ( int j = 0; j < n2; j++) {
if (T[j] != S[i + j]) {
chk = false ;
break ;
}
}
if (chk) {
ans += (i + 1 - last)
* (n1 - (i + n2 - 1));
last = i + 1;
}
}
cout << ans;
}
int main()
{
string S = "dabc" , T = "ab" ;
findOccurrences(S, T);
}
|
Java
class GFG{
static void findOccurrences(String S, String T)
{
int n1 = S.length();
int n2 = T.length();
int ans = 0 ;
int last = 0 ;
for ( int i = 0 ; i <= n1 - n2; i++)
{
boolean chk = true ;
for ( int j = 0 ; j < n2; j++)
{
if (T.charAt(j) != S.charAt(i + j))
{
chk = false ;
break ;
}
}
if (chk)
{
ans += (i + 1 - last)
* (n1 - (i + n2 - 1 ));
last = i + 1 ;
}
}
System.out.println(ans);
}
public static void main (String[] args)
{
String S = "dabc" , T = "ab" ;
findOccurrences(S, T);
}
}
|
Python3
def findOccurrences(S, T):
n1 = len (S)
n2 = len (T)
ans = 0
last = 0
for i in range (n1 - n2 + 1 ):
chk = True
for j in range (n2):
if (T[j] ! = S[i + j]):
chk = False
break
if (chk):
ans + = (i + 1 - last) * (n1 - (i + n2 - 1 ))
last = i + 1
print (ans)
if __name__ = = '__main__' :
S,T = "dabc" , "ab"
findOccurrences(S, T)
|
C#
using System;
class GFG
{
static void findOccurrences(String S, String T)
{
int n1 = S.Length;
int n2 = T.Length;
int ans = 0;
int last = 0;
for ( int i = 0; i <= n1 - n2; i++)
{
bool chk = true ;
for ( int j = 0; j < n2; j++)
{
if (T[j] != S[i + j])
{
chk = false ;
break ;
}
}
if (chk)
{
ans += (i + 1 - last)
* (n1 - (i + n2 - 1));
last = i + 1;
}
}
Console.WriteLine(ans);
}
public static void Main(String[] args)
{
String S = "dabc" , T = "ab" ;
findOccurrences(S, T);
}
}
|
Javascript
<script>
function findOccurrences(S, T)
{
let n1 = S.length;
let n2 = T.length;
let ans = 0;
let last = 0;
for (let i = 0; i <= n1 - n2; i++)
{
let chk = true ;
for (let j = 0; j < n2; j++)
{
if (T[j] != S[i + j])
{
chk = false ;
break ;
}
}
if (chk)
{
ans += (i + 1 - last)
* (n1 - (i + n2 - 1));
last = i + 1;
}
}
document.write(ans);
}
let S = "dabc" , T = "ab" ;
findOccurrences(S, T);
</script>
|
Time Complexity: O(N*M) since two nested loops are used where N and M are the lengths of given strings.
Auxiliary Space: O(1) since no extra array is used the space occupied by the algorithm is constant.