forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1257.java
More file actions
18 lines (18 loc) · 661 Bytes
/
1257.java
File metadata and controls
18 lines (18 loc) · 661 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public String findSmallestRegion(List<List<String>> regions, String region1, String region2) {
Map<String, String> parent = new HashMap();
for (List<String> region : regions) {
Iterator<String> iter = region.iterator();
String fa = iter.next();
while (iter.hasNext()) {
parent.put(iter.next(), fa);
}
}
String n1 = region1, n2 = region2;
while (!n1.equals(n2)) {
n1 = parent.containsKey(n1) ? parent.get(n1) : region2;
n2 = parent.containsKey(n2) ? parent.get(n2) : region1;
}
return n1;
}
}