Concise Java Solution


  • 0
    public class Solution {
        public String[] findRestaurant(String[] list1, String[] list2) {
            int sum = Integer.MAX_VALUE;
            String res = "";
            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 rest = list2[i];
                int temp = i + map.getOrDefault(rest, 0); 
                if (map.containsKey(rest) && temp <= sum) {
                    res = temp < sum ? rest : res + "," + rest;
                    sum = temp;
                }
            }
            return res.split(",");
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.