for Robot Artificial Inteligence

51. GroupAnagrams

|

class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        unordered_map<string , vector<string>> mp;
        for(string s : strs)
        {
            string t = s;
            sort(t.begin(),t.end());
            mp[t].push_back(s);
        }
        vector<vector<string>> anagrams;
        for(auto m : mp)
        {
            anagrams.push_back(m.second);
        }
        return anagrams;
    }
};

Comments