Number of substrings that start with “geeks” and end with “for”
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; } |
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 |
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 |
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 |
3
Recommended Posts:
- Given a binary string, count number of substrings that start and end with 1.
- Different substrings in a string that start and end with given strings
- Number of substrings divisible by 8 but not by 3
- Number of substrings of a string
- Number of balanced parenthesis substrings
- Count Substrings with equal number of 0s, 1s and 2s
- Number of even substrings in a string of digits
- Number of substrings with count of each character as k
- Number of substrings of one string present in other
- Sum of all substrings of a string representing a number | Set 1
- Number of substrings divisible by 6 in a string of integers
- Count number of substrings with numeric value greater than X
- Count number of distinct substrings of a given length
- Number of substrings with odd decimal value in a binary string
- Count number of substrings with exactly k distinct characters
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

