-
Notifications
You must be signed in to change notification settings - Fork 3
/
737.cpp
63 lines (59 loc) · 1.91 KB
/
737.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class DisjointSet {
private:
vector<int> parent;
vector<int> rank;
public:
DisjointSet(int size) {
parent.resize(size);
rank.resize(size);
for (int i = 0; i < size; ++i) parent[i] = i;
}
int find(int x) {
if (parent[x] != x) {
parent[x] = find(parent[x]);
}
return parent[x];
}
void join(int x, int y) {
int pX = find(x);
int pY = find(y);
if (pX == pY) return;
if (rank[pX] > rank[pY]) {
parent[pY] = pX;
}
else if (rank[pX] < rank[pY]) {
parent[pX] = pY;
}
else {
parent[pY] = pX;
rank[pX]++;
}
}
};
class Solution {
public:
bool areSentencesSimilarTwo(vector<string>& sentence1, vector<string>& sentence2, vector<vector<string>>& similarPairs) {
if (sentence1.size() != sentence2.size()) return false;
unordered_map<string, int> word2idx;
int index = 0;
for (auto& pair : similarPairs) {
if (word2idx.find(pair[0]) == word2idx.end()) word2idx[pair[0]] = index++;
if (word2idx.find(pair[1]) == word2idx.end()) word2idx[pair[1]] = index++;
}
DisjointSet* disjointSet = new DisjointSet(index);
for (auto& pair : similarPairs) {
disjointSet->join(word2idx[pair[0]], word2idx[pair[1]]);
}
int n = sentence1.size();
for (int i = 0; i < n; ++i) {
if (sentence1[i] != sentence2[i]) {
if (word2idx.find(sentence1[i]) == word2idx.end()) return false;
if (word2idx.find(sentence2[i]) == word2idx.end()) return false;
int idx1 = word2idx[sentence1[i]];
int idx2 = word2idx[sentence2[i]];
if (disjointSet->find(idx1) != disjointSet->find(idx2)) return false;
}
}
return true;
}
};