package com.fishercoder.solutions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 599. Minimum Index Sum of Two Lists
 *
 Suppose Andy and Doris want to choose a restaurant for dinner,
 and they both have a list of favorite restaurants represented by strings.
 You need to help them find out their common interest with the least list index sum.
 If there is a choice tie between answers, output all of them with no order requirement. You could assume there always exists an answer.

 Example 1:
 Input:
 ["Shogun", "Tapioca Express", "Burger King", "KFC"]
 ["Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"]
 Output: ["Shogun"]
 Explanation: The only restaurant they both like is "Shogun".

 Example 2:
 Input:
 ["Shogun", "Tapioca Express", "Burger King", "KFC"]
 ["KFC", "Shogun", "Burger King"]
 Output: ["Shogun"]
 Explanation: The restaurant they both like and have the least index sum is "Shogun" with index sum 1 (0+1).

 Note:
 The length of both lists will be in the range of [1, 1000].
 The length of strings in both lists will be in the range of [1, 30].
 The index is starting from 0 to the list length minus 1.
 No duplicates in both lists.
 */
public class _599 {
    public String[] findRestaurant(String[] list1, String[] list2) {
        if (list1 == null || list2 == null) {
            return new String[0];
        }
        Map<String, Integer> map1 = putIntoMap(list1);
        Map<String, Integer> map2 = putIntoMap(list2);
        int leastIndexSum = Integer.MAX_VALUE;
        List<String> resultList = new ArrayList<>();
        for (String key1 : map1.keySet()) {
            if (map2.containsKey(key1)) {
                int indexSum = map1.get(key1) + map2.get(key1);
                if (indexSum < leastIndexSum) {
                    resultList.clear();
                    resultList.add(key1);
                    leastIndexSum = indexSum;
                } else if (indexSum == leastIndexSum) {
                    resultList.add(key1);
                }
            }
        }
        String[] result = new String[resultList.size()];
        for (int i = 0; i < resultList.size(); i++) {
            result[i] = resultList.get(i);
        }
        return result;
    }

    private Map<String, Integer> putIntoMap(String[] list) {
        Map<String, Integer> map = new HashMap<>();
        for (int i = 0; i < list.length; i++) {
            map.put(list[i], i);
        }
        return map;
    }
}

@@ -1259,16 +1259,56 @@
  _599 %7B%0A
+    public static class Solution1 %7B%0A    
     publ
@@ -1364,24 +1364,28 @@
 %5B%5D list2) %7B%0A
+    
         if (
@@ -1426,24 +1426,28 @@
             
+    
 return new S
@@ -1464,26 +1464,33 @@
         
-%7D%0A
+    %7D%0A   
         
 Map%3CStri
@@ -1473,32 +1473,33 @@
    %7D%0A           
+ 
 Map%3CString, Inte
@@ -1525,24 +1525,28 @@
 Map(list1);%0A
+    
         Map%3C
@@ -1592,24 +1592,28 @@
 2);%0A        
+ 
+   
 int leastInd
@@ -1647,16 +1647,20 @@
         
+  
+  
 List%3CStr
@@ -1692,24 +1692,28 @@
 ayList%3C%3E();%0A
+    
         for 
@@ -1752,24 +1752,28 @@
             
+    
 if (map2.con
@@ -1802,24 +1802,28 @@
             
+ 
+   
 int indexSum
@@ -1870,24 +1870,28 @@
             
+    
 if (indexSum
@@ -1922,32 +1922,36 @@
                 
+   
+ 
 resultList.clear
@@ -1950,24 +1950,28 @@
 st.clear();%0A
+    
             
@@ -2020,16 +2020,20 @@
         
+    
 leastInd
@@ -2066,16 +2066,20 @@
         
+  
+  
 %7D else i
@@ -2122,32 +2122,36 @@
                 
+    
 resultList.add(k
@@ -2164,32 +2164,36 @@
                 
+    
 %7D%0A            %7D%0A
@@ -2190,26 +2190,33 @@
         
+ 
-%7D%0A
+   %7D%0A   
         
 %7D%0A      
@@ -2199,34 +2199,39 @@
    %7D%0A           
+ 
 %7D%0A
+    
         String%5B%5D
@@ -2271,32 +2271,36 @@
 ize()%5D;%0A        
+    
 for (int i = 0; 
@@ -2337,24 +2337,28 @@
             
+    
 result%5Bi%5D = 
@@ -2376,34 +2376,42 @@
 get(i);%0A        
+   
+ 
 %7D%0A
+    
         return r
@@ -2417,16 +2417,20 @@
 result;%0A
+    
     %7D%0A%0A 
@@ -2432,16 +2432,20 @@
  %7D%0A%0A    
+  
+  
 private 
@@ -2489,24 +2489,28 @@
 g%5B%5D list) %7B%0A
+    
         Map%3C
@@ -2549,32 +2549,36 @@
 ap%3C%3E();%0A        
+    
 for (int i = 0; 
@@ -2613,16 +2613,20 @@
         
+ 
+   
 map.put(
@@ -2638,34 +2638,42 @@
 i%5D, i);%0A        
-%7D%0A
+    %7D%0A    
         return m
@@ -2676,16 +2676,26 @@
 rn map;%0A
+        %7D%0A
     %7D%0A%7D%0A

refactor 599