Day 6 of 30-Day LeetCode Challenge
1 min readApr 15, 2020
Given an array of strings, group anagrams together.
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string>> result;
unordered_map<string, vector<string>> mp;
for(int i=0;i<strs.size();++i){ //O(nklogk)
string curr = strs[i];
sort(curr.begin(), curr.end());
mp[curr].push_back(strs[i]);
}
for(auto i:mp){
result.push_back(i.second);
}
return result;
}
};