Given a string **str** consisting of lowercase English alphabets, the task is to find the count of substrings that start with **“geeks”** and end with **“for”**.

**Examples:**

Input:str = “geeksforgeeksisforgeeks”

Output:3

“geeksfor”, “geeksforgeeksisfor” and “geeksisfor”

are the only valid substrings.

Input:str = “geeksforgeeks”

Output:1

**Naive approach:** First set the counter to 0 then iterate over the string and whenever the substring **“geeks”** is encountered, from the very next indices again iterate over the string and try to find the substring **“for”**. If **“for”** is present then increment the counter and finally print it.

**Efficient approach:** Set two counter for the substrings **“geeks”** and **“for”**, say c1 and c2. On iterating, whenever the substring **“geeks”** is encountered, increment c1 and whenever **“for”** is encountered, set **c2 = c2 + c1**. This is because every occurrence of **“geeks”** will make a valid substring with the current found **“for”**. Finally print **c2**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Function to return the count ` `// of required substrings ` `int` `countSubStr(string s, ` `int` `n) ` `{ ` ` ` `int` `c1 = 0, c2 = 0; ` ` ` ` ` `// For every index of the string ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// If the substring starting at ` ` ` `// the current index is "geeks" ` ` ` `if` `(s.substr(i, 5) == ` `"geeks"` `) ` ` ` `c1++; ` ` ` ` ` `// If the substring is "for" ` ` ` `if` `(s.substr(i, 3) == ` `"for"` `) ` ` ` `c2 = c2 + c1; ` ` ` `} ` ` ` ` ` `return` `c2; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string s = ` `"geeksforgeeksisforgeeks"` `; ` ` ` `int` `n = s.size(); ` ` ` ` ` `cout << countSubStr(s, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

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

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the count ` `# of required substrings ` `def` `countSubStr(s, n) : ` ` ` ` ` `c1 ` `=` `0` `; c2 ` `=` `0` `; ` ` ` ` ` `# For every index of the string ` ` ` `for` `i ` `in` `range` `(n) : ` ` ` ` ` `# If the substring starting at ` ` ` `# the current index is "geeks" ` ` ` `if` `(s[i : i ` `+` `5` `] ` `=` `=` `"geeks"` `) : ` ` ` `c1 ` `+` `=` `1` `; ` ` ` ` ` `# If the substring is "for" ` ` ` `if` `(s[i :i` `+` `3` `] ` `=` `=` `"for"` `) : ` ` ` `c2 ` `=` `c2 ` `+` `c1; ` ` ` ` ` `return` `c2; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `s ` `=` `"geeksforgeeksisforgeeks"` `; ` ` ` `n ` `=` `len` `(s); ` ` ` ` ` `print` `(countSubStr(s, n)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `public` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the count ` ` ` `// of required substrings ` ` ` `static` `int` `countSubStr(String s, ` `int` `n) ` ` ` `{ ` ` ` `int` `c1 = 0, c2 = 0; ` ` ` ` ` `// For every index of the string ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` ` ` `// If the substring starting at ` ` ` `// the current index is "geeks" ` ` ` `if` `(i < n - 5 && ` ` ` `"geeks"` `.Equals(s.Substring(i, 5))) ` ` ` `{ ` ` ` `c1++; ` ` ` `} ` ` ` ` ` `// If the substring is "for" ` ` ` `if` `(i < n - 3 && ` ` ` `"for"` `.Equals(s.Substring(i, 3))) ` ` ` `{ ` ` ` `c2 = c2 + c1; ` ` ` `} ` ` ` `} ` ` ` `return` `c2; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main(String[] args) ` ` ` `{ ` ` ` `String s = ` `"geeksforgeeksisforgeeks"` `; ` ` ` `int` `n = s.Length; ` ` ` `Console.WriteLine(countSubStr(s, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

3