VOOZH about

URL: https://www.geeksforgeeks.org/dsa/count-of-substrings-of-a-string-containing-another-given-string-as-a-substring/

⇱ Count of substrings of a string containing another given string as a substring - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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

Last Updated : 23 Jul, 2025

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:


Output: 
4

 

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

Comment
Article Tags: