Count of substrings of a string containing another given string as a substring | C ++ program for the above approach ; Function to store all substrings of S ; Stores the substrings of S ; Pick start point in outer loop and lengths of different strings for a given starting point ; Return the array containing substrings of S ; Function to check if a string is present in another string ; Check if target is in the string str or not ; Function to count the substring of S containing T in it as substring ; Store all substrings of S in the array v [ ] ; Store required count of substrings ; Iterate through all the substrings of S ; If string T is present in the current substring , then increment the ans ; Print the answer ; Driver code ; Function Call
#include <bits/stdc++.h> NEW_LINE 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 ; }