Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- vector<vector<string>> groupAnagrams(vector<string>& strs) {
- unordered_map<string, vector<string>> m;
- for(int i = 0; i<strs.size(); i++){
- string temp = strs[i];
- sort(temp.begin(), temp.end());
- if(m.find(temp)==m.end()){
- vector<string> xyz = {strs[i]};
- m.insert({temp,xyz});
- //m.insert({temp,{strs[i]}}); => sort way
- }
- else{
- vector<string> v = m.find(temp)->second;
- v.push_back(strs[i]);
- m.find(temp)->second = v;
- }
- }
- vector<vector<string>> ans;
- for(auto it = m.begin(); it!=m.end(); it++){
- ans.push_back(it->second);
- }
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement