leetcode 1087 Bigram分词
题目描述:
给出第一个词 first 和第二个词 second,考虑在某些文本 text 中可能以 “first second third” 形式出现的情况,其中 second 紧随 first 出现,third 紧随 second 出现。
对于每种这样的情况,将第三个词 “third” 添加到答案中,并返回答案。
示例 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”]
解题思路:
first和second字符串拼接(first和second之间还要有一个空格),查找子字符串,获取下标,然后裁剪原来的字符串。边界情况的考虑,参见代码
class Solution {
public:
vector<string> findOcurrences(string text, string first, string second) {
vector<string> res;
string fs = first + " " + second; //拼接字符串
int fsLength = fs.size();
while (text.find(fs) != string::npos){
int pos = text.find(fs);
// 字符串裁剪的条件
if((pos == 0) || ((pos-1 >= 0) && (text[pos-1] == ' ')))
text = text.substr(pos+fsLength);
else {
text = text.substr(pos+fsLength);
continue;
}
// third单词的寻找
string tmp = "";
bool flag = false;
for (int i=0; i<text.size(); i++){
if (i == 0){
if(text[i] != ' ') {
flag = true;
break;
}
}
else if(text[i] != ' ') {
tmp += text[i];
}
else if(text[i] == ' ') {
break;
}
}
if (!flag){
if (tmp != "")
res.push_back(tmp);
}
}
return res;
}
};