Count of substrings of a string containing another given string as a substring
Last Updated :
01 Jun, 2021
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:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to store all substrings of S
vector<string> subString(string s, int n)
{
// Stores the substrings of S
vector<string> 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 string
int 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 substring
void countSubstrings(string& S, string& T)
{
// Store all substrings of S in
// the array v[]
vector<string> 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 code
int main()
{
string S = "dabc";
string T = "ab";
// Function Call
countSubstrings(S, T);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to store all subStrings of S
static Vector<String> subString(String s, int n)
{
// Stores the subStrings of S
Vector<String> 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 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[]
Vector<String> 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 code
public 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 S
def 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 string
def 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 substring
def 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 code
if __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 approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to store all subStrings of S
static List<string> subString(string s, int n)
{
// Stores the subStrings of S
List<string> v = new List<string>();
// 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<string> 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
<script>
// JavaScript program for the above approach
// Function to store all substrings of S
function subString(s, n)
{
// Stores the substrings of S
var v = [];
var i,len;
// Pick start point in outer loop
// and lengths of different strings
// for a given starting point
for (i = 0; i < n; i++) {
for (len = 1;
len <= n - i; len++) {
var find = s.substr(i, len);
v.push(find);
}
}
// Return the array containing
// substrings of S
return v;
}
// Function to check if a string is
// present in another string
function IsPresent(str, target)
{
// Check if target is in the
// string str or not
if (str.includes(target))
{
return 1;
}
return -1;
}
// Function to count the substring of S
// containing T in it as substring
function countSubstrings(S, T)
{
// Store all substrings of S in
// the array v[]
var v = subString(S, S.length);
// Store required count of substrings
var ans = 0;
var i;
// Iterate through all the
// substrings of S
for (i=0;i<v.length;i++) {
// If string T is present in the
// current substring, then
// increment the ans
if (IsPresent(v[i], T) != -1) {
ans++;
}
}
// Print the answer
document.write(ans);
}
// Driver code
var S = "dabc";
var T = "ab";
// Function Call
countSubstrings(S, T);
</script>
Time Complexity: O(N2)
Auxiliary Space: O(N2)
Similar Reads
Count of substrings of a string containing another given string as a substring | Set 2 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: 4Explanation:Substrings of S containing T as a substring are: S[0, 2] = âdabâS[1, 2] = âabâS[1, 3
8 min read
Count of substrings from given Ternary strings containing characters at least once Given string str of size N consisting of only 0, 1, and 2, the task is to find the number of substrings that consists of characters 0, 1, and 2 at least once. Examples: Input: str = "0122"Output: 2Explanation:There exists 2 substrings such that the substrings has characters 0, 1, 2 at least once is
6 min read
Count number of substrings of a string consisting of same characters Given a string. The task is to find out the number of substrings consisting of the same characters. Examples: Input: abba Output: 5 The desired substrings are {a}, {b}, {b}, {a}, {bb} Input: bbbcbb Output: 10 Approach: It is known for a string of length n, there are a total of n*(n+1)/2 number of su
6 min read
Count of substrings containing only the given character Given a string S and a character C, the task is to count the number of substrings of S that contains only the character C.Examples: Input: S = "0110111", C = '1' Output: 9 Explanation: The substrings containing only '1' are: "1" â 5 times "11" â 3 times "111" â 1 time Hence, the count is 9. Input: S
6 min read
Count of substrings formed using a given set of characters only Given a string str and an array arr[] of K characters, the task is to find the number of substrings of str that contain characters only from the given character array arr[]. Note: The string str and the arr[] contain only lowercase alphabets. Examples: Input: S = "abcb", K = 2, charArray[] = {'a', '
8 min read
Count of substrings of a given Binary string with all characters same Given binary string str containing only 0 and 1, the task is to find the number of sub-strings containing only 1s and 0s respectively, i.e all characters same. Examples: Input: str = â011âOutput: 4Explanation: Three sub-strings are "1", "1", "11" which have only 1 in them, and one substring is there
10 min read