# Python, Straightforward with Explanation

• Say the lists are `A` and `B`. Let `Aindex[element]` be the index of that element in A. For every index, value pair (j, v) in B, we have some candidate sum-of-indexes i + j, where i = Aindex[v] if it exists. If the candidate sum is better, it becomes our new answer; if the candidate sums are the same, then we append to our answer.

``````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
``````

• This post is deleted!

• ``````    def findRestaurantTwoDicts(self, list1, list2):
"""
:type list1: List[str]
:type list2: List[str]
:rtype: List[str]
"""
dic1, dic2 = {}, {}
res_sum = len(list1)+len(list2)
for i, s in enumerate(list1):
dic1[s] = i
for i, s in enumerate(list2):
if s in dic1:
tmp_sum = dic1[s] + i
dic2[tmp_sum] = dic2.get(tmp_sum, []) + [s]
res_sum = min(tmp_sum, res_sum)
return dic2[res_sum]
``````

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