forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
44 lines (42 loc) · 1.31 KB
/
Solution.java
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
class Solution {
private int[] p;
public boolean areSentencesSimilarTwo(
String[] sentence1, String[] sentence2, List<List<String>> similarPairs) {
if (sentence1.length != sentence2.length) {
return false;
}
int n = similarPairs.size();
p = new int[n << 1];
for (int i = 0; i < p.length; ++i) {
p[i] = i;
}
Map<String, Integer> words = new HashMap<>();
int idx = 0;
for (List<String> e : similarPairs) {
String a = e.get(0), b = e.get(1);
if (!words.containsKey(a)) {
words.put(a, idx++);
}
if (!words.containsKey(b)) {
words.put(b, idx++);
}
p[find(words.get(a))] = find(words.get(b));
}
for (int i = 0; i < sentence1.length; ++i) {
if (Objects.equals(sentence1[i], sentence2[i])) {
continue;
}
if (!words.containsKey(sentence1[i]) || !words.containsKey(sentence2[i])
|| find(words.get(sentence1[i])) != find(words.get(sentence2[i]))) {
return false;
}
}
return true;
}
private int find(int x) {
if (p[x] != x) {
p[x] = find(p[x]);
}
return p[x];
}
}