给定两个字符串str1和str2,任务是使用递归计算“ str1”在“ str1”中出现的次数。
例子:
Input : str1 = "geeksforgeeks", str2 = "geek"
Output : 2
Input: kanekihiishishi
Output: 3
假设问题有n个部分,以考虑已经完成的n-1个部分的方式划分问题,然后将要执行的操作限制为仅一个部分。从而,将递归方法分为两种情况,即基本情况和递归情况。
在此特定问题中,基本情况涉及以下事实:如果str1的长度小于str2的长度。
现在,在讨论递归情况时,将str1的第一个子字符串与str2进行比较,然后对剩余的str1进行递归。
C++
// Recursive C++ program for counting number of substrings
#include
#include
using namespace std;
// Recursive function to count
// the number of occurrences of "hi" in str.
int countSubstrig(string str1, string str2)
{
int n1 = str1.length();
int n2 = str2.length();
// Base Case
if (n1 == 0 || n1 < n2)
return 0;
// Recursive Case
// Checking if the first substring matches
if (str1.substr(0, n2).compare(str2) == 0)
return countSubstrig(str1.substr(n2-1), str2) + 1;
// Otherwise, return the count from
// the remaining index
return countSubstrig(str1.substr(n2-1), str2);
}
// Driver function
int main()
{
string str1 = "geeksforgeeks", str2 = "geeks";
cout << countSubstrig(str1, str2) << endl;
str1 = "hikakashi", str2 = "hi";
cout << countSubstrig(str1, str2) << endl;
return 0;
}
Java
// Recursive Java program for
// counting number of substrings
class GFG
{
// Recursive function to
// count the number of
// occurrences of "hi" in str.
static int countSubstrig(String str1,
String str2)
{
int n1 = str1.length();
int n2 = str2.length();
// Base Case
if (n1 == 0 || n1 < n2)
return 0;
// Recursive Case
// Checking if the first
// substring matches
if (str1.substring(0, n2).equals(str2))
return countSubstrig(str1.substring(n2 - 1),
str2) + 1;
// Otherwise, return the count
// from the remaining index
return countSubstrig(str1.substring(n2 - 1),
str2);
}
// Driver Code
public static void main(String args[])
{
String str1 = "geeksforgeeks",
str2 = "geeks";
System.out.println(countSubstrig(str1,
str2));
str1 = "hikakashi";
str2 = "hi";
System.out.println(countSubstrig(str1,
str2));
}
}
// This code is contributed
// by Arnab Kundu
Python3
# Recursive Python3 program for
# counting number of substrings
# Recursive function to
# count the number of
# occurrences of "hi" in str.
def countSubstrig(str1, str2):
n1 = len(str1);
n2 = len(str2);
# Base Case
if (n1 == 0 or n1 < n2):
return 0;
# Recursive Case
# Checking if the first
# substring matches
if (str1[0 : n2] == str2):
return countSubstrig(str1[n2 - 1:],
str2) + 1;
# Otherwise, return the count
# from the remaining index
return countSubstrig(str1[n2 - 1:],
str2);
# Driver Code
if __name__ == '__main__':
str1 = "geeksforgeeks";
str2 = "geeks";
print(countSubstrig(str1, str2));
str1 = "hikakashi";
str2 = "hi";
print(countSubstrig(str1, str2));
# This code is contributed by Princi Singh
C#
// Recursive C# program for
// counting number of substrings
using System;
class GFG
{
// Recursive function to
// count the number of
// occurrences of "hi" in str.
static int countSubstrig(String str1,
String str2)
{
int n1 = str1.Length;
int n2 = str2.Length;
// Base Case
if (n1 == 0 || n1 < n2)
return 0;
// Recursive Case
// Checking if the first
// substring matches
if (str1.Substring(0, n2).Equals(str2))
return countSubstrig(str1.Substring(n2 - 1),
str2) + 1;
// Otherwise, return the
// count from the remaining
// index
return countSubstrig(str1.Substring(n2 - 1),
str2);
}
// Driver Code
public static void Main()
{
string str1 = "geeksforgeeks",
str2 = "geeks";
Console.Write(countSubstrig(str1,
str2));
Console.Write("\n");
str1 = "hikakashi";
str2 = "hi";
Console.Write(countSubstrig(str1,
str2));
}
}
// This code is contributed
// by Smita
输出:
2
2