C++ Solution using 2 maps


  • 0
     vector<string> findRestaurant(vector<string>& A, vector<string>& B) {
            
            unordered_map<string,int> umap;
            
            for(int i=0;i<A.size();i++)
                umap[A[i]] = i;
            
            map<int,vector<string> > umap_2;
            for(int i=0;i<B.size();i++)
            {
                if(umap.find(B[i]) != umap.end())
                {
                    int index = umap[B[i]];
                    umap_2[index+i].push_back(B[i]);
                }
            }
            
            for(auto itr = umap_2.begin(); itr != umap_2.end(); itr++)
                return itr->second;
        }
    

Log in to reply
 

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