当前位置:网站首页>LeetCode-1078. Bigram participle

LeetCode-1078. Bigram participle

2022-06-12 06:22:00 Border wanderer

Give the first word  first And the second word  second, Consider in some texts  text  It is possible that "first second third" What happens in the form of , among  second  In the wake of  first  appear ,third  In the wake of  second  appear .

For each such case , Put the third word "third" Add 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"]
 

Tips :

1 <= text.length <= 1000
text  It consists of lowercase English letters and spaces
text All words in the are separated by Single space character Separate
1 <= first.length, second.length <= 10
first and  second  It's made up of lowercase letters

#include<iostream>
#include<vector>
#include<unordered_map>
using namespace std;
class Solution {
public:
    vector<string> findOcurrences(string text, string first, string second) {
        unordered_map<string, bool> hash;
        vector<string> vec;
        text.push_back(' ');
        string key = first + ' ' + second;
        cout << "key:" << key << endl;
        splitstr(vec, text, key);
        return vec;
    }


    /*  It needs to be counted twice  */
    void splitstr(vector<string>& vec, string &text, string& key) {
        int start = 0;
        int laststart = 0;
        int index = 0;
        string tmpstr;
        int count = 0;                        /*  Count the number of spaces  */
        for (char c : text) {
            if (c == ' ') {
                count++;
                if (count == 1) {
                    laststart = index;
                }
                if (count == 2) {
                    tmpstr = text.substr(start, index - start);
                    start = laststart + 1;
                    laststart = index;
                    count = 1;
                    cout << "tmpstr1:" << tmpstr << endl;
                    findThird(vec, tmpstr, text, key,index);
                }
            }

            index++;
        }
    }


    void findThird(vector<string>& vec, string &tmpstr, string&text, string& key, int index) {
        if (tmpstr == key) {
            cout << "in" << endl;
            for (int i = index + 1; i < text.size(); i++) {
                if (text[i] == ' ') {
                    string third = text.substr(index + 1, i - index - 1);
                    cout << "third:" << third << endl;
                    vec.emplace_back(third);
                    break;
                }
            }
        }
    }

};

原网站

版权声明
本文为[Border wanderer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010609443475.html