Clean easy C++ solution - unordered_map beats 45%


  • 0
    A
    class Solution {
    public:
     vector<string> findRestaurant(vector<string>& list1, vector<string>& list2){
            unordered_map<int,vector<string>> map; // count -> [common strings]
            int min = list1.size() + list2.size();
            
            for(int i=0;i<list2.size();i++){             
                auto it = find(list1.begin(),list1.end(),list2[i]); 
                if(it !=list1.end()){ // common string found                
                    auto i1 = distance(list1.begin(),it); //common string index
                    if((i1 + i) < min) min = i1 + i;
                    map[i1 + i].push_back(list2[i]);               
                }             
            }
            return map[min];
        }
    };
    

Log in to reply
 

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