Python Hashmap solution


  • 0
    Y
        def findRestaurant(self, list1, list2):
            """
            :type list1: List[str]
            :type list2: List[str]
            :rtype: List[str]
            """
            n1,n2=len(list1),len(list2)
            mp1,mp2={},{}
            for i in xrange(n1):
                mp1[list1[i]]=i
            for i in xrange(n2):
                mp2[list2[i]]=i
     
            res=[]
            idx=[]
            for s in mp1:
                if mp2.has_key(s):
                    idx.append((mp1[s]+mp2[s],mp1[s]))
    
            if len(idx)>0:
                idx.sort()
                sum1=idx[0][0]
                res.append(list1[idx[0][1]])
                for i in xrange(1,len(idx)):
                    if idx[i][0]!=sum1:
                        break
                    res.append(list1[idx[i][1]])
    
            return res
    

Log in to reply
 

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