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++
#include <iostream>
using namespace std;
int countSubStr(string s, int n)
{
int c1 = 0, c2 = 0;
for ( int i = 0; i < n; i++) {
if (s.substr(i, 5) == "geeks" )
c1++;
if (s.substr(i, 3) == "for" )
c2 = c2 + c1;
}
return c2;
}
int main()
{
string s = "geeksforgeeksisforgeeks" ;
int n = s.size();
cout << countSubStr(s, n);
return 0;
}
|
Java
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));
}
}
|
Python3
def countSubStr(s, n) :
c1 = 0 ; c2 = 0 ;
for i in range (n) :
if (s[i : i + 5 ] = = "geeks" ) :
c1 + = 1 ;
if (s[i :i + 3 ] = = "for" ) :
c2 = c2 + c1;
return c2;
if __name__ = = "__main__" :
s = "geeksforgeeksisforgeeks" ;
n = len (s);
print (countSubStr(s, n));
|
C#
using System;
public 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, 5)))
{
c1++;
}
if (i < n - 3 &&
"for" .Equals(s.Substring(i, 3)))
{
c2 = c2 + c1;
}
}
return c2;
}
public static void Main(String[] args)
{
String s = "geeksforgeeksisforgeeks" ;
int n = s.Length;
Console.WriteLine(countSubStr(s, n));
}
}
|
Javascript
<script>
function countSubStr(s, n)
{
var c1 = 0, c2 = 0;
for ( var i = 0; i < n; i++) {
if (s.substring(i, i+5) == "geeks" )
c1++;
if (s.substring(i,i+ 3) == "for" )
c2 = c2 + c1;
}
return c2;
}
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)
Last Updated :
12 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...