Simplest Python Solution Beats 95% of Submissions


  • 0
    M
    class Solution(object):
        def findRestaurant(self, list1, list2):
            """
            :type list1: List[str]
            :type list2: List[str]
            :rtype: List[str]
            """
            d = {}
            l2 = set(list2)
            for i, val in enumerate(list1):
                if val in l2:
                    d[val] = i
            
            for i, val in enumerate(list2):
                if val in d:
                    d[val] += i
            
            m = min(d.values())
            
            return [r for r in d if d[r] == m]
    

Log in to reply
 

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