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 function
function countSubstrig ( str1 , str2 ) { let n1 = str1 . length ; let n2 = str2 . length ; if ( n1 == 0 n1 < n2 ) return 0 ; if ( str1 . substr ( 0 , n2 ) == ( str2 ) ) return countSubstrig ( str1 . substr ( n2 - 1 ) , str2 ) + 1 ; return countSubstrig ( str1 . substr ( n2 - 1 ) , str2 ) ; } let str1 = " " , str2 = " " ; document . write ( countSubstrig ( str1 , str2 ) , ' ' ) ; str1 = " " , str2 = " " ; document . write ( countSubstrig ( str1 , str2 ) , ' ' ) ;