1078.Bigram分词
链接:1078.Bigram分词
难度:Easy
标签:字符串
简介:对于每种这样的情况,将第三个词 "third" 添加到答案中,并返回答案。
题解 1 - cpp
- 编辑时间:2021-12-26
- 内存消耗:6.5MB
- 编程语言:cpp
- 解法介绍:分割字符串。
class Solution {
   public:
    vector<string> split(string text) {
        vector<string> ans;
        for (int i = 0; i < text.size(); i++) {
            int end = i;
            while (end < text.size() && text[end] != ' ') end++;
            ans.push_back(text.substr(i, end - i));
            i = end;
        }
        return ans;
    }
    vector<string> findOcurrences(string text, string first, string second) {
        vector<string> ans;
        vector<string> list = split(text);
        for (int i = 0; i < list.size() - 2; i++) {
            string str = list[i];
            if (str == first && list[i + 1] == second) {
                ans.push_back(list[i + 2]);
            }
        }
        return ans;
    }
};