![]() |
VOOZH | about |
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:
- S[0, 2] = “dab”
- S[1, 2] = “ab”
- S[1, 3] = “abc”
- 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:
4
Time Complexity: O(N2)
Auxiliary Space: O(N2)