forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
occurrences-after-bigram.cpp
44 lines (43 loc) · 1.14 KB
/
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Time: O(n)
// Space: O(1)
class Solution {
public:
vector<string> findOcurrences(string text, string first, string second) {
vector<string> result;
first.push_back(' ');
second.push_back(' ');
string third;
int i = 0, j = 0, k = 0;
while (k < text.length()) {
auto c = text[k++];
if (i != first.length()) {
if (c == first[i]) {
++i;
} else {
i = 0;
}
continue;
}
if (j != second.length()) {
if (c == second[j]) {
++j;
} else {
k -= j + 1;
i = 0, j = 0;
}
continue;
}
if (c != ' ') {
third.push_back(c);
continue;
}
k -= second.length() + third.length() + 1;
i = 0, j = 0;
result.emplace_back(move(third));
}
if (!third.empty()) {
result.emplace_back(move(third));
}
return result;
}
};