Skip to content

Latest commit

 

History

History
22 lines (18 loc) · 649 Bytes

37.md

File metadata and controls

22 lines (18 loc) · 649 Bytes
vector<vector<string>> groupAnagrams(vector<string>& strs) {
    unordered_map<string, int> mp;
        vector<vector<string>> ans; 

        for (int i = 0; i < strs.size(); i++) {
            string temp = strs[i];
            sort(temp.begin(), temp.end());
            if (mp.find(temp) != mp.end()) {
                ans[mp[temp]].push_back(strs[i]);
            } else {
                mp[temp] = ans.size();
                ans.push_back({strs[i]});
                
            }
        }

        return ans;
    }