Skip to content

Instantly share code, notes, and snippets.

@shitu13
Created May 18, 2024 09:34
Show Gist options
  • Save shitu13/7e18b53a90201da3ed5f6b31a89217f7 to your computer and use it in GitHub Desktop.
Save shitu13/7e18b53a90201da3ed5f6b31a89217f7 to your computer and use it in GitHub Desktop.
Group Anagrams
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
int n = strs.size();
unordered_map<string, vector<string>> mp;
for (int i = 0; i < n; i++) {
string temp = strs[i];
sort(temp.begin(), temp.end());
mp[temp].push_back(strs[i]);
}
vector<vector<string>> res;
for (auto it : mp) {
res.push_back(it.second);
}
return res;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment