GeeksforGeeks App
Open App
Browser
Continue

# Count of substrings of a string containing another given string as a substring

Given two strings S and T, the task is to count the number of substrings of S that contains 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:

1. S[0, 2] = “dab”
2. S[1, 2] = “ab”
3. S[1, 3] = “abc”
4. S[0, 3] = “dabc”

Input: S = “hshshshs” T = “hs”
Output: 25

Approach: The idea is to generate all the substrings of S and check for each substring if it contains T in it or not. Follow the steps below to solve the problem:

Below is the implementation of the above approach:

## C++

 // C++ program for the above approach#include using namespace std; // Function to store all substrings of Svector subString(string s, int n){    // Stores the substrings of S    vector v;     // Pick start point in outer loop    // and lengths of different strings    // for a given starting point    for (int i = 0; i < n; i++) {         for (int len = 1;             len <= n - i; len++) {             string find = s.substr(i, len);            v.push_back(find);        }    }     // Return the array containing    // substrings of S    return v;} // Function to check if a string is// present in another stringint IsPresent(string& str, string& target){    // Check if target is in the    // string str or not    if (str.find(target)        != string::npos) {        return 1;    }     return -1;} // Function to count the substring of S// containing T in it as substringvoid countSubstrings(string& S, string& T){     // Store all substrings of S in    // the array v[]    vector v = subString(S, S.length());     // Store required count of substrings    int ans = 0;     // Iterate through all the    // substrings of S    for (auto it : v) {         // If string T is present in the        // current substring, then        // increment the ans        if (IsPresent(it, T) != -1) {            ans++;        }    }     // Print the answer    cout << ans;} // Driver codeint main(){    string S = "dabc";    string T = "ab";     // Function Call    countSubstrings(S, T);     return 0;}

## Java

 // Java program for the above approachimport java.util.*;class GFG{ // Function to store all subStrings of Sstatic Vector subString(String s, int n){       // Stores the subStrings of S    Vector v = new Vector<>();     // Pick start point in outer loop    // and lengths of different Strings    // for a given starting point    for (int i = 0; i < n; i++)    {         for (int len = 1;             len <= n - i; len++)        {             String find = s.substring(i, i + len);            v.add(find);        }    }     // Return the array containing    // subStrings of S    return v;} // Function to check if a String is// present in another Stringstatic int IsPresent(String str, String target){       // Check if target is in the    // String str or not    if (str.contains(target))    {        return 1;    }    return -1;} // Function to count the subString of S// containing T in it as subStringstatic void countSubStrings(String S, String T){     // Store all subStrings of S in    // the array v[]    Vector v = subString(S, S.length());     // Store required count of subStrings    int ans = 0;     // Iterate through all the    // subStrings of S    for (String it : v)    {         // If String T is present in the        // current subString, then        // increment the ans        if (IsPresent(it, T) != -1)        {            ans++;        }    }     // Print the answer    System.out.print(ans);} // Driver codepublic static void main(String[] args){    String S = "dabc";    String T = "ab";     // Function Call    countSubStrings(S, T); }} // This code is contributed by Princi Singh

## Python3

 # Python3 program for the above approach # Function to store all substrings of Sdef subString(s, n):         # Stores the substrings of S    v = []     # Pick start point in outer loop    # and lengths of different strings    # for a given starting point    for i in range(n):         for len in range(1, n - i + 1):             find = s[i : i + len]            v.append(find)     # Return the array containing    # substrings of S    return v # Function to check if a is# present in another stringdef IsPresent(str, target):         # Check if target is in the    # str or not    if (target in str):        return 1     return -1 # Function to count the subof S# containing T in it as substringdef countSubstrings(S, T):     # Store all substrings of S in    # the array v[]    v = subString(S, len(S))     # Store required count of substrings    ans = 0     # Iterate through all the    # substrings of S    for it in v:         # If T is present in the        # current substring, then        # increment the ans        if (IsPresent(it, T) != -1):            ans += 1     # Print the answer    print(ans) # Driver codeif __name__ == '__main__':    S = "dabc"    T = "ab"     #Function Call    countSubstrings(S, T) # This code is contributed by mohit kumar 29

## C#

 // C# program for the above approachusing System;using System.Collections.Generic;class GFG{     // Function to store all subStrings of S    static List subString(string s, int n)    {         // Stores the subStrings of S        List v = new List();         // Pick start point in outer loop        // and lengths of different Strings        // for a given starting point        for (int i = 0; i < n; i++)        {            for (int len = 1; len <= n - i; len++)            {                string find = s.Substring(i, len);                v.Add(find);            }        }         // Return the array containing        // subStrings of S        return v;    }     // Function to check if a String is    // present in another String    static int IsPresent(string str, string target)    {         // Check if target is in the        // String str or not        if (str.Contains(target))        {            return 1;        }        return -1;    }     // Function to count the subString of S    // containing T in it as subString    static void countSubStrings(string S, string T)    {         // Store all subStrings of S in        // the array v[]        List v = subString(S, S.Length);         // Store required count of subStrings        int ans = 0;         // Iterate through all the        // subStrings of S        foreach(string it in v)        {             // If String T is present in the            // current subString, then            // increment the ans            if (IsPresent(it, T) != -1)            {                ans++;            }        }         // Print the answer        Console.WriteLine(ans);    }     // Driver code    public static void Main(string[] args)    {        string S = "dabc";        string T = "ab";         // Function Call        countSubStrings(S, T);    }} // This code is contributed by chitranayal

## Javascript



Output:

4

Time Complexity: O(N2)
Auxiliary Space: O(N2)

My Personal Notes arrow_drop_up