Number of substrings that start with "geeks" and end with "for"
Last Updated :
12 Sep, 2022
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
JavaScript
<script>
// JavaScript implementation of the approach
// Function to return the count
// of required substrings
function countSubStr(s, n)
{
var c1 = 0, c2 = 0;
// For every index of the string
for (var i = 0; i < n; i++) {
// If the substring starting at
// the current index is "geeks"
if (s.substring(i, i+5) == "geeks")
c1++;
// If the substring is "for"
if (s.substring(i,i+ 3) == "for")
c2 = c2 + c1;
}
return c2;
}
// Driver code
var s = "geeksforgeeksisforgeeks";
var n = s.length;
document.write( countSubStr(s, n));
</script>
Time Complexity: O(N), where N is the length of the given string
Auxiliary Space: O(1)
Similar Reads
Get K-th letter of the decoded string formed by repeating substrings Given a string S containing letter and digit and an integer K where, 2\leq S.length() \leq 100 and 1\leq K \leq 10^{9} . The task is to return the K-th letter of the new string S'.The new string S' is formed from old string S by following steps: 1. If the character read is a letter, that letter is a
6 min read
Different substrings in a string that start and end with given strings Given a string s and two other strings begin and end, find the number of different substrings in the string which begin and end with the given begin and end strings. Examples: Input : s = "geeksforgeeks" begin = "geeks" end = "for" Output : 1 Input : s = "vishakha" begin = "h" end = "a" Output : 2 T
9 min read
Count of strings whose prefix match with the given string to a given length k Given an array of strings arr[] and given some queries where each query consists of a string str and an integer k. The task is to find the count of strings in arr[] whose prefix of length k matches with the k length prefix of str.Examples: Input: arr[] = {"abba", "abbb", "abbc", "abbd", "abaa", "abc
15+ min read
Print all strings in the given array that occur as the substring in the given string Given an array of string arr[] and a string str, the task is to print all the strings in arr[] that occur as a substring in str. Example: Input: str ="geeksforgeeks", arr[] ={ "forg", "geek", "ek", "dog", "sfor"}Output: forggeekeksforExplanation: The strings "forg", "geek", "ek" and "sfor" occur as
5 min read
Count K-Length Substrings With No Repeated Characters Given a string S and an integer k, the task is to return the number of substrings in S of length k with no repeated characters.Example:Input: S = "geeksforgeeks", k = 5Output: 4Explanation: There are 4 substrings, they are: 'eksfo', 'ksfor', 'sforg', 'forge'.Input: S = "home", k = 5Output: 0Explanat
6 min read
Print all Substrings of length n possible from the given String Given a string str and an integer N, the task is to print all possible sub-strings of length N. Examples: Input: str = âgeeksforgeeksâ, N = 3Output: gee eek eks ksf sfo for org rge gee eek eksExplanations: All possible sub-strings of length 3 are âgeeâ, âeekâ, âeksâ, âksfâ, âsfoâ, âforâ, âorgâ, ârge
8 min read