给出两个字符串,你需要找到缺少的字符串
样例:
给一个字符串 str1 = This is an example, 给出另一个字符串 str2 = is example
返回 [“This”, “an”]
#ifndef C684_H
#define C684_H
#include<iostream>
#include<vector>
#include<string>
#include<map>
using namespace std;
class Solution {
public:
/*
* @param : a given string
* @param : another given string
* @return: An array of missing string
*/
vector<string> missingString(string str1, string str2) {
// Write your code here
vector<string> res;
if (str1.empty())
return res;
map<string, int> m;
string str;
bool flag = true;
for (auto c : str1) //将字符串str1分割成单词,并按顺序存入res中
{
if (isspace(c))
{
if (!flag)
{
res.push_back(str);
str.clear();
flag = true;
}
}
else
{
str += c;
flag = false;
}
}
if (!str.empty())
res.push_back(str);
flag = true;
str.clear();
for (auto t : str2) //res中删除所有str2分割出来的单词
{
if (isspace(t))
{
if (!flag)
{
findStr(str, res);
str.clear();
flag = true;
}
}
else
{
str += t;
flag = false;
}
}
if (!str.empty())
findStr(str, res);
return res;
}
//在v中删除所有与str相等的元素
void findStr(string str, vector<string> &v)
{
vector<string>::iterator it1;
vector<string>::iterator it2;
for (it1 = v.begin(); it1 != v.end();)
{
if (str == *it1)
{
it2 = it1;
it1 = v.erase(it2);
}
else
it1++;
}
}
};
#endif