forked from keineahnung2345/leetcode-cpp-practices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1078. Occurrences After Bigram.cpp
29 lines (27 loc) · 1.04 KB
/
1078. Occurrences After Bigram.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
//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Occurrences After Bigram.
//Memory Usage: 8.8 MB, less than 100.00% of C++ online submissions for Occurrences After Bigram.
class Solution {
public:
std::vector<std::string> string_split(std::string str, std::string delimiter){
size_t pos = 0;
std::string token;
std::vector<std::string> result;
while ((pos = str.find(delimiter)) != std::string::npos) {
token = str.substr(0, pos);
result.push_back(token);
str.erase(0, pos + delimiter.length());
}
result.push_back(str);
return result;
}
vector<string> findOcurrences(string text, string first, string second) {
vector<string> split = string_split(text, " ");
vector<string> ans;
for(int i = 0; i < split.size() - 1; i++){
if(split[i] == first && split[i+1] == second && i+2 < split.size()){
ans.push_back(split[i+2]);
}
}
return ans;
}
};