class Solution {
public String[] findRestaurant(String[] list1, String[] list2) {
int indexSum = Integer.MAX_VALUE;
List<String> ans = new LinkedList<>();
Map<String, Integer> map = new HashMap<>();
for (int i = 0; i < list1.length; i++) map.put(list1[i], i);
for (int i = 0; i < list2.length; i++) {
String restaurant = list2[i];
if (map.containsKey(restaurant)) {
int sum = i + map.get(restaurant);
if (sum == indexSum) ans.add(restaurant);
else if (sum < indexSum) {
indexSum = sum;
ans = new LinkedList<>();
ans.add(restaurant);
}
}
}
return ans.toArray(new String[ans.size()]);
}
}