304 North Cardinal St.
Dorchester Center, MA 02124

# Minimum Index Sum of Two Lists LeetCode Solution

## Problem – 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: list1 = ["Shogun","Tapioca Express","Burger King","KFC"], list2 = ["Piatti","The Grill at Torrey Pines","Hungry Hunter Steakhouse","Shogun"]
Output: ["Shogun"]
Explanation: The only restaurant they both like is "Shogun".``````

Example 2:

``````Input: list1 = ["Shogun","Tapioca Express","Burger King","KFC"], list2 = ["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).``````

Constraints:

• `1 <= list1.length, list2.length <= 1000`
• `1 <= list1[i].length, list2[i].length <= 30`
• `list1[i]` and `list2[i]` consist of spaces `' '` and English letters.
• All the stings of `list1` are unique.
• All the stings of `list2` are unique.

### Minimum Index Sum of Two Lists LeetCode Solution in Java

``````public String[] findRestaurant(String[] list1, String[] list2) {
Map<String, Integer> map = new HashMap<>();
int minSum = Integer.MAX_VALUE;
for (int i=0;i<list1.length;i++) map.put(list1[i], i);
for (int i=0;i<list2.length;i++) {
Integer j = map.get(list2[i]);
if (j != null && i + j <= minSum) {
if (i + j < minSum) { res.clear(); minSum = i+j; }
}
}
return res.toArray(new String[res.size()]);
}
``````

### Minimum Index Sum of Two Lists LeetCode Solution in Python

``````def findRestaurant(self, A, B):
Aindex = {u: i for i, u in enumerate(A)}
best, ans = 1e9, []

for j, v in enumerate(B):
i = Aindex.get(v, 1e9)
if i + j < best:
best = i + j
ans = [v]
elif i + j == best:
ans.append(v)
return ans
``````

### Minimum Index Sum of Two Lists LeetCode Solution in C++

``````    vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) {
vector<string>res;
unordered_map<string,int>m;
int min = INT_MAX;
for(int i = 0; i < list1.size(); i++) m[list1[i]] = i;
for(int i = 0; i < list2.size(); i++)
if(m.count(list2[i]) != 0)
if(m[list2[i]] + i < min) min = m[list2[i]] + i, res.clear(), res.push_back(list2[i]);
else if(m[list2[i]] + i == min) res.push_back(list2[i]);
return res;
}
``````
##### Minimum Index Sum of Two Lists LeetCode Solution Review:

In our experience, we suggest you solve this Minimum Index Sum of Two Lists LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Minimum Index Sum of Two Lists LeetCode Solution

Find on LeetCode

##### Conclusion:

I hope this Minimum Index Sum of Two Lists LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions