forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountofPinS.cpp
33 lines (30 loc) · 1.07 KB
/
CountofPinS.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using namespace std;
// Problem Statement:
// Given a string, write a recursive function, to print the occurrences of string p in the string s(Overlap is allowed)
// Example :
// 1. Input: p = "aaa", s = "aaaaaa"
// Output: 4
// 2. Input: p = "hi", s = "hihihithi"
// Output: 4
void check_p_in_s(string p, string s, int stri, int count){
if(stri == s.size()){ //When all the characters in string s are checked, print the count
cout << count;
return;
}
if(p == s.substr(stri, p.size())){ //When we find string p in string s, we increase the count and increment string index by 1
return check_p_in_s(p, s, stri + 1, count + 1);
}
else{ //When string p does not match the substring in s, we just move to the next character without increasing the count
return check_p_in_s(p, s, stri + 1, count);
}
}
int main(){
string p, s;
cout << "Enter the main string";
cin >> s;
cout << "Enter the string that you want to count in the main string";
cin >> p;
check_p_in_s(p, s, 0, 0);
return 0;
}