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
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 ) ; } }