Solution using unordered map


  • 0
    T

    code using unordered map

    vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) {
            unordered_map<string, int> hash;
            for (int i=0; i < list1.size(); i++){
                hash[list1[i]] = i;
            }
            unordered_map<int, vector<string>> allResult;
            int sumInd = 3000;
            for (int i=0; i < list2.size(); i++){
                if (hash.find(list2[i]) != hash.end()){
                    if (sumInd >= i + hash[list2[i]]){
                        sumInd = i + hash[list2[i]];
                        allResult[sumInd].push_back(list2[i]);
                    }
                }
            }
            return allResult[sumInd];
        }
    

Log in to reply
 

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