vector<string> findOcurrences(string text, string first, string second) {
vector<string> words;
int s =0, e =0, len = text.length();
while (true) {
while (s < len && text[s] ==' ') {
s++;
}
if (s >= len) {
break;
}
e = s +1;
while (e < len && text[e] !=' ') {
e++;
}
words.push_back(text.substr(s, e - s));
s = e +1;
}
vector<string> ret;
for (int i =2; i < words.size(); i++) {
if (words[i -2] == first && words[i -1] == second) {
ret.push_back(words[i]);
}
}
return ret;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
vector<string> findOcurrences(string text, string first, string second) {
stringstream ss(text);
string str;
vector<string> strs;
while (ss >> str) {
strs.push_back(str);
}
vector<string> ans;
for (int i =0; i +2< strs.size(); i ++) {
if (strs[i] == first && strs[i +1] == second) {
ans.push_back(strs[i +2]);
}
}
return ans;
}