Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
sentence-similarity.cpp 1.05 KB
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2017-11-26 18:17 +08:00 . Create sentence-similarity.cpp
// Time: O(n + p)
// Space: O(p)
class Solution {
public:
bool areSentencesSimilar(vector<string>& words1, vector<string>& words2, vector<pair<string, string>> pairs) {
if (words1.size() != words2.size()) {
return false;
}
unordered_set<pair<string, string>, PairHash<string>> lookup;
for (const auto& pair : pairs) {
lookup.emplace(pair.first, pair.second);
lookup.emplace(pair.second, pair.first);
}
for (int i = 0; i < words1.size(); ++i) {
if (words1[i] != words2[i] &&
!lookup.count(make_pair(words1[i], words2[i]))) {
return false;
}
}
return true;
}
private:
template <typename T>
struct PairHash {
size_t operator()(const pair<T, T>& p) const {
size_t seed = 0;
seed ^= std::hash<T>{}(p.first) + 0x9e3779b9 + (seed<<6) + (seed>>2);
seed ^= std::hash<T>{}(p.second) + 0x9e3779b9 + (seed<<6) + (seed>>2);
return seed;
}
};
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助