1078. Bigram word

Links: https://leetcode-cn.com/problems/occurrences-after-bigram/

The first word is given first and the second word second, to consider the possibility of the situation "first second third" of the form, which first appeared in second followed by some text in the text, third following the second appearance.

For each of these cases, the third word "third" added to the answers, and return the answer.

 

Example 1:

输入:text = "alice is a good girl she is a good student", first = "a", second = "good"
输出:["girl","student"]
示例 2:

输入:text = "we will we will rock you", first = "we", second = "will"
输出:["we","rock"]
 

prompt:

. 1 <= text.length <= 1000
text by a number of space-separated words, each word by lowercase letters
. 1 <= first.length, second.length <10 =
First and second lowercase English letters

class Solution {
public:
    vector<string> findOcurrences(string text, string first, string second) {
        vector<string> newText;
        vector<string> ans;
        int len = text.size();
        string temp = "";

        for(int i = 0; i <= len; i++)
        {
            if(text[i] != ' ' && text[i] != '\0') temp.push_back(text[i]);
            else
            {
                newText.push_back(temp);
                temp = "";
            } 
        }

        int len2 = newText.size();
        for(int i = 0; i < len2 - 2; i++)
        {
            if(newText[i] == first && newText[i + 1] == second) ans.push_back(newText[i + 2]);
        } 

        return ans;
    }
};

The spaces in the word string are stored into newText, and then traversing newText judge, attention After third word is determined to stop the countdown.

string to the tail additive elements str.push_back ()

 

Published 58 original articles · won praise 6 · views 7047

Guess you like

Origin blog.csdn.net/weixin_43569916/article/details/104003705