My Java Solution


  • 0
    W
    public String[] findRestaurant(String[] list1, String[] list2) {
    	HashMap<String, Integer> hm = new HashMap<>(list1.length);
    	for (int i = 0; i < list1.length; ++i)
    		hm.put(list1[i], i);
    
    	int min = Integer.MAX_VALUE;
    	String result = "";
    
    	for (int i = 0; i < list2.length; ++i) {
    		if (min < i)
    			break;
    		if (hm.containsKey(list2[i])) {
    			if (hm.get(list2[i]) + i < min) {
    				min = hm.get(list2[i]) + i;
    				result = list2[i];
    			} else if (hm.get(list2[i]) + i == min)
    				result += "," + list2[i];
    		}
    	}
    	return result.split(",");
    }

Log in to reply
 

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