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
Explanation: The occurrences of str2 in str1 are starting at index {0, 8}
Input: str1 = “aaaaa”, str2 = “aaa”
Output: 3
Explanation: The occurrences of str2 in str1 are starting at index {0,1,2
Approach: We have already discussed other approaches in our previous article but here we are going to solve this problem using recursion.
Algorithm:
- If size of string str2 is greater then string str1 or size of string str1 is 0 then, return 0.
- Otherwise, Check if string str2 is present in str1 as substring or not.
- if present then, increment the count of occurrence and recursively call for other substring.
- else, recursively call for other substring.
- return count from every recursively call as answer.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <string>
using namespace std;
int countSubstring(string str1, string str2)
{
int n1 = str1.length();
int n2 = str2.length();
if (n1 == 0 || n1 < n2)
return 0;
if (str1.substr(0, n2).compare(str2) == 0)
return countSubstring(str1.substr(1), str2) + 1;
return countSubstring(str1.substr(1), str2);
}
int main()
{
string str1 = "geeksforgeeks" , str2 = "geeks" ;
cout << countSubstring(str1, str2) << endl;
str1 = "aaaaa" , str2 = "aaa" ;
cout << countSubstring(str1, str2) << endl;
return 0;
}
|
Java
class GFG
{
static int countSubstring(String str1,
String str2)
{
int n1 = str1.length();
int n2 = str2.length();
if (n1 == 0 || n1 < n2)
return 0 ;
if (str1.substring( 0 , n2).equals(str2))
return countSubstring(str1.substring( 1 ),
str2) + 1 ;
return countSubstring(str1.substring( 1 ),
str2);
}
public static void main(String args[])
{
String str1 = "geeksforgeeks" ,
str2 = "geeks" ;
System.out.println(countSubstring(str1,
str2));
str1 = "aaaaa" ;
str2 = "aaa" ;
System.out.println(countSubstring(str1,
str2));
}
}
|
Python3
def countSubstring(str1, str2):
n1 = len (str1);
n2 = len (str2);
if (n1 = = 0 or n1 < n2):
return 0 ;
if (str1[ 0 : n2] = = str2):
return countSubstring(str1[ 1 :],
str2) + 1 ;
return countSubstring(str1[ 1 :],
str2);
if __name__ = = '__main__' :
str1 = "geeksforgeeks" ;
str2 = "geeks" ;
print (countSubstring(str1, str2));
str1 = "aaaaa" ;
str2 = "aaa" ;
print (countSubstring(str1, str2));
|
C#
using System;
class GFG
{
static int countSubstring(String str1,
String str2)
{
int n1 = str1.Length;
int n2 = str2.Length;
if (n1 == 0 || n1 < n2)
return 0;
if (str1.Substring(0, n2).Equals(str2))
return countSubstring(str1.Substring(1),
str2) + 1;
return countSubstring(str1.Substring(1),
str2);
}
public static void Main()
{
string str1 = "geeksforgeeks" ,
str2 = "geeks" ;
Console.Write(countSubstring(str1,
str2));
Console.Write( "\n" );
str1 = "aaaaa" ;
str2 = "aaa" ;
Console.Write(countSubstring(str1,
str2));
}
}
|
Javascript
<script>
function countSubstring( str1, str2){
let n1 = str1.length;
let n2 = str2.length;
if (n1 == 0 || n1 < n2)
return 0;
if (str1.substr(0, n2) == (str2))
return countSubstring(str1.substr(1), str2) + 1;
return countSubstring(str1.substr(1), str2);
}
let str1 = "geeksforgeeks" , str2 = "geeks" ;
document.write( countSubstring(str1, str2), '<br>' );
str1 = "aaaaa" , str2 = "aaa" ;
document.write( countSubstring(str1, str2), '<br>' );
</script>
|
Complexity Analysis:
- Time Complexity: O(N2) , (i.e. N is maximum length between str1 and str2)
- Auxiliary Space: O(1)
Last Updated :
26 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...