Python, 3 lines with one dict and one list


  • 0
    Y
    class Solution(object):
        def findRestaurant(self, list1, list2):
            """
            :type list1: List[str]
            :type list2: List[str]
            :rtype: List[str]
            """
            common = {string: index + list2.index(string) for index, string in enumerate(list1) if string in list2} # get common restaurant dict
            common = sorted([(value,key) for (key,value) in common.items()]) # sort common dict to list by sum of index
            common_least = [item[1] for item in common if item[0] == common[0][0]] # get common restaurant by having common least index sum
            return common_least
    

Log in to reply
 

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