给定一个由小写英文字母组成的字符串str ,任务是查找以“ geeks”开头和以“ for”结尾的子字符串数。
例子:
Input: str = “geeksforgeeksisforgeeks”
Output: 3
“geeksfor”, “geeksforgeeksisfor” and “geeksisfor”
are the only valid substrings.
Input: str = “geeksforgeeks”
Output: 1
天真的方法:首先将计数器设置为0,然后遍历字符串,并且每当遇到子字符串“ geeks”时,从下一个索引开始再次遍历字符串,并尝试找到子字符串“ for” 。如果显示“ for” ,则增加计数器并最终打印出来。
高效的方法:为子字符串“ geeks”和“ for”设置两个计数器,例如c1和c2。进行迭代时,每当遇到子字符串“ geeks”时,就递增c1;每当遇到“ for”时,将c2 = c2 + c1设置为1 。这是因为每次出现的“怪胎”都将使用当前找到的“ for”组成一个有效的子字符串。最后打印c2 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
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
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。