Count of substrings of a string containing another given string as a substring | Java 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
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 ) ; } }