判断两个句子是否相似,细节题
class Solution {
public:
bool areSentencesSimilar(vector<string>& words1, vector<string>& words2, vector<pair<string, string>> pairs) {
if(words1.size()!=words2.size())
return false;
map<string, set<string>> pairsMap;
for(int i=0;i<pairs.size();i++)
{
string a = pairs[i].first;
string b = pairs[i].second;
if(pairsMap.find(a)==pairsMap.end()){
set<string> temp;
temp.insert(b);
pairsMap[a] = temp;
}
else
pairsMap[a].insert(b);
}
for(int i=0;i<words1.size();i++){
string a = words1[i];
string b = words2[i];
if(a!=b&&pairsMap[a].find(b)==pairsMap[a].end()&&pairsMap[b].find(a)==pairsMap[b].end())
return false;
}
return true;
}
};