Number of substrings that start with " geeks " and end with " for " | Java implementation of the approach ; Function to return the count of required substrings ; For every index of the string ; If the substring starting at the current index is " geeks " ; If the substring is " for " ; Driver code
class GFG { static int countSubStr ( String s , int n ) { int c1 = 0 , c2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i < n - 5 && " geeks " . equals ( s . substring ( i , i + 5 ) ) ) { c1 ++ ; } if ( i < n - 3 && " for " . equals ( s . substring ( i , i + 3 ) ) ) { c2 = c2 + c1 ; } } return c2 ; } public static void main ( String [ ] args ) { String s = " geeksforgeeksisforgeeks " ; int n = s . length ( ) ; System . out . println ( countSubStr ( s , n ) ) ; } }