# Count occurrences of a substring recursively

Given two strings str1 and str2, the task is to count the number of times “str2” occurs in “str1” using recursion.

Examples:

```Input : str1 = "geeksforgeeks", str2 = "geek"
Output : 2

Input: kanekihiishishi
Output: 3
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Suppose the problem has n parts, divide the problem in such a way that considers n-1 parts already done after which the operation to be performed is limited to only one part. Thereby, dividing the recursion approach into two cases i.e. base case and the recursive case.
In this particular problem, the base case involves the fact that if the length of str1 is less than that of str2.

Now, talking about the recursive case, compare first substring of str1 with str2 and recur for remaining 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 `

Output:

```2
2
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.