给出两个字符串,你需要找到缺少的字符串
样例
样例 1:
输入 : str1 = "This is an example", str2 = "is example"
输出 : ["This", "an"]
注意事项
输出时字符串顺序为输入字符串内的顺序
思路:把s1,s2每一个单词分别放进不同的vector中,在比较两个vector中的单词是否能相互找到,若不能,放进结果容器中。
注意:find函数若无法找到返回end()。
class Solution {
public:
/**
* @param str1: a given string
* @param str2: another given string
* @return: An array of missing string
*/
vector<string> missingString(string &str1, string &str2) {
// Write your code here
int len1=str1.size();
int len2=str2.size();
vector<string>s1;
vector<string>s2;
vector<string>result;
string temp="";
for (int i = 0; i < len1; i++) {
/* code */
if(str1[i]==' ') {s1.push_back(temp);temp="";}
else temp+=str1[i];
if(i==len1-1) {s1.push_back(temp);temp="";}
}
for (int i = 0; i < len2; i++) {
/* code */
if(str2[i]==' ') {s2.push_back(temp);temp="";}
else temp+=str2[i];
if(i==len2-1) {s2.push_back(temp);temp="";}
}
for (auto judge : s1) {
/* code */
if(find(s2.begin(),s2.end(),judge)==s2.end()) result.push_back(judge);
}
return result;
}
};```