51. GroupAnagrams
17 Jul 2020 | Daily Algorithms
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;
}
};
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