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.

 

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"]
 

Note:

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.

这题没啥难的,切词之后找词序即可。

class OccurrencesAfterBigram : public Solution {
public:
    void Exec() {

    }
    void StringSplit(const std::string &str, std::vector<std::string> &out,
                     const std::string &sep) {
        std::string::size_type pos1, pos2;
        pos2 = str.find(sep);
        pos1 = 0;
        while (std::string::npos != pos2) {
            out.push_back(str.substr(pos1, pos2 - pos1));

            pos1 = pos2 + sep.size();
            pos2 = str.find(sep, pos1);
        }
        if (pos1 != str.length())
            out.push_back(str.substr(pos1));
    }
    vector<string> findOcurrences(string text, string first, string second) {
        vector<string> substrs;
        StringSplit(text, substrs, " ");

        vector<string> res;
        for (int i = 0; i < substrs.size(); i++) {
            if (substrs[i] == first && i + 2 < substrs.size()) {
                if (substrs[i + 1] == second) {
                    res.push_back(substrs[i + 2]);
                }
            }
        }
        return res;
    }
};

共 0 条回复
暂时没有人回复哦,赶紧抢沙发
发表新回复

作者

sryan
today is a good day