#include<unordered_map> classSolution { public: vector<string> findRestaurant(vector<string>& list1, vector<string>& list2){ unordered_map<string,int> map; for (int i = 0; i < list1.size(); i ++){ map[list1[i]]= i; }
unordered_map<string,int> ans; for (int i = 0; i < list2.size(); i ++){ if (map.count(list2[i]) > 0){ ans[list2[i]] = map[list2[i]] + i; } } vector<string> res; int minin = 9999; for (auto i = ans.begin();i != ans.end();i ++){ if (i -> second < minin){ res.clear(); minin = i -> second; res.push_back(i -> first); }elseif (i -> second == minin){ res.push_back(i -> first); } } return res; } };