Count occurrences of a substring recursively | Recursive function to count the number of occurrences of " hi " in str . ; Base Case ; Recursive Case Checking if the first substring matches ; Otherwise , return the count from the remaining index ; Driver Code
def countSubstrig ( str1 , str2 ) : NEW_LINE INDENT n1 = len ( str1 ) ; NEW_LINE n2 = len ( str2 ) ; NEW_LINE if ( n1 == 0 or n1 < n2 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( str1 [ 0 : n2 ] == str2 ) : NEW_LINE INDENT return countSubstrig ( str1 [ n2 - 1 : ] , str2 ) + 1 ; NEW_LINE DEDENT return countSubstrig ( str1 [ n2 - 1 : ] , str2 ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = " geeksforgeeks " ; NEW_LINE str2 = " geeks " ; NEW_LINE print ( countSubstrig ( str1 , str2 ) ) ; NEW_LINE str1 = " hikakashi " ; NEW_LINE str2 = " hi " ; NEW_LINE print ( countSubstrig ( str1 , str2 ) ) ; NEW_LINE DEDENT