Minimum Index Sum of Two Lists

Problem Id: 599 Difficulty: Easy


Intuition

Solution


class Solution:
    def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
        map1 = {}
        for i in range(len(list1)):
            map1[list1[i]] = i
        ans = []
        s = len(list1) + len(list2)
        for i in range(len(list2)):
            if list2[i] in map1:
                sum_i = i + map1[list2[i]]
                if sum_i < s:
                    ans = [list2[i]]
                    s = sum_i
                elif sum_i == s:
                    ans.append(list2[i])
        return ans