1078. Occurrences After Bigram - easy
문제
Given words first and second, consider occurrences in some text of the form “first second third”, where second comes immediately after first, and third comes immediately after second.
For each such occurrence, add “third” to the answer, and return the answer.
제한사항
- 1 <= text.length <= 1000
- text consists of space separated words, where each word consists of lowercase English letters.
- 1 <= first.length, second.length <= 10
- first and second consist of lowercase English letters.
입출력 예
1
2
3
4
5
6
7
Example 1:
Input: text = "alice is a good girl she is a good student", first = "a", second = "good"
Output: ["girl","student"]
Example 2:
Input: text = "we will we will rock you", first = "we", second = "will"
Output: ["we","rock"]
풀이
- String
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
vector<string> findOcurrences(string text, string first, string second) {
vector<string> answer;
vector<string> splitText;
string token;
stringstream ss(text);
// 공백을 기준으로 문자열을 tokenize
while(getline(ss, token, ' ')){
splitText.push_back(token);
}
// tokenize한 문자들을 검사
for(int i = 0; i < splitText.size() - 2; i++){
if(splitText[i] == first && splitText[i + 1] == second){
answer.push_back(splitText[i+2]);
}
}
return answer;
}
};