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++
#include <bits/stdc++.h>
using namespace std;
vector<string> subString(string s, int n)
{
vector<string> v;
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 v;
}
int IsPresent(string& str, string& target)
{
if (str.find(target)
!= string::npos) {
return 1;
}
return -1;
}
void countSubstrings(string& S, string& T)
{
vector<string> v = subString(S, S.length());
int ans = 0;
for ( auto it : v) {
if (IsPresent(it, T) != -1) {
ans++;
}
}
cout << ans;
}
int main()
{
string S = "dabc" ;
string T = "ab" ;
countSubstrings(S, T);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static Vector<String> subString(String s, int n)
{
Vector<String> v = new Vector<>();
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 v;
}
static int IsPresent(String str, String target)
{
if (str.contains(target))
{
return 1 ;
}
return - 1 ;
}
static void countSubStrings(String S, String T)
{
Vector<String> v = subString(S, S.length());
int ans = 0 ;
for (String it : v)
{
if (IsPresent(it, T) != - 1 )
{
ans++;
}
}
System.out.print(ans);
}
public static void main(String[] args)
{
String S = "dabc" ;
String T = "ab" ;
countSubStrings(S, T);
}
}
|
Python3
def subString(s, n):
v = []
for i in range (n):
for len in range ( 1 , n - i + 1 ):
find = s[i : i + len ]
v.append(find)
return v
def IsPresent( str , target):
if (target in str ):
return 1
return - 1
def countSubstrings(S, T):
v = subString(S, len (S))
ans = 0
for it in v:
if (IsPresent(it, T) ! = - 1 ):
ans + = 1
print (ans)
if __name__ = = '__main__' :
S = "dabc"
T = "ab"
countSubstrings(S, T)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static List< string > subString( string s, int n)
{
List< string > v = new List< string >();
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 v;
}
static int IsPresent( string str, string target)
{
if (str.Contains(target))
{
return 1;
}
return -1;
}
static void countSubStrings( string S, string T)
{
List< string > v = subString(S, S.Length);
int ans = 0;
foreach ( string it in v)
{
if (IsPresent(it, T) != -1)
{
ans++;
}
}
Console.WriteLine(ans);
}
public static void Main( string [] args)
{
string S = "dabc" ;
string T = "ab" ;
countSubStrings(S, T);
}
}
|
Javascript
<script>
function subString(s, n)
{
var v = [];
var i,len;
for (i = 0; i < n; i++) {
for (len = 1;
len <= n - i; len++) {
var find = s.substr(i, len);
v.push(find);
}
}
return v;
}
function IsPresent(str, target)
{
if (str.includes(target))
{
return 1;
}
return -1;
}
function countSubstrings(S, T)
{
var v = subString(S, S.length);
var ans = 0;
var i;
for (i=0;i<v.length;i++) {
if (IsPresent(v[i], T) != -1) {
ans++;
}
}
document.write(ans);
}
var S = "dabc" ;
var T = "ab" ;
countSubstrings(S, T);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N2)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...