Java solution


  • 0
                    List<String> rtn = new ArrayList<>();
    		Map<String, Integer> map = new HashMap<>();
    		for (int i = 0; i < list1.length; ++i)
    			map.put(list1[i], i);
    
    		int minSum = Integer.MAX_VALUE;
    		for (int i = 0; i < list2.length; ++i) {
    			final Integer index = map.get(list2[i]);
    			if (index != null) {
    				if (i + index < minSum) {
    					rtn.clear();
    					rtn.add(list2[i]);
    					minSum = i + index;
    				} else if (i + index == minSum) {
    					rtn.add(list2[i]);
    				}
    			}
    		}
    
    		return rtn.toArray(new String[rtn.size()]);
    

Log in to reply
 

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