Python using Intersection of 2 sets


  • 0
    J
    class Solution(object):
        def findRestaurant(self, list1, list2):
            """
            :type list1: List[str]
            :type list2: List[str]
            :rtype: List[str]
            """
            
            dict_list1 = {k:v for v,k in enumerate(list1)}
            dict_list2 = {k:v for v,k in enumerate(list2)}
            res_index = len(list1) + len(list2)
            res = []
            
            
            intersection = list(set(dict_list1.keys()) & set(dict_list2.keys()))
            if len(intersection) == 1:
                return intersection
            else:
                for restaurant in intersection:
                    index = dict_list1[restaurant] + dict_list2[restaurant]
                    if index < res_index:
                        res_index = index
                        res = [restaurant]
                    elif (index == res_index):    
                        res.append(restaurant)
            return res    
    

Log in to reply
 

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