forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentence-similarity.cpp
34 lines (32 loc) · 1.05 KB
/
sentence-similarity.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// 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;
}
};
};