Given two strings str1 and str2, the task is to count the number of times “str2” occurs in “str1” using recursion.
Input : str1 = "geeksforgeeks", str2 = "geek" Output : 2 Input: kanekihiishishi Output: 3
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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Decode a string recursively encoded as count followed by substring
- Lexicographically smallest substring with maximum occurrences containing a's and b's only
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Count Occurrences of Anagrams
- Count of occurrences of a "1(0+)1" pattern in a string
- Count occurrences of a word in string
- Count occurrences of a character in a repeated string
- Count of occurrences of each prefix in a string using modified KMP algorithm
- Longest substring with count of 1s more than 0s
- Count of distinct characters in a substring by given range for Q queries
- Maximize count of 0s in left and 1s in right substring by splitting given Binary string
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Maximum number of set bits count in a K-size substring of a Binary String
- Count substring of Binary string such that each character belongs to a palindrome of size greater than 1
- Count occurrences of a string that can be constructed from another given string
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Minimum length of substring whose rotation generates a palindromic substring
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Insertion in a Trie recursively
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.