Created
May 18, 2024 09:34
-
-
Save shitu13/7e18b53a90201da3ed5f6b31a89217f7 to your computer and use it in GitHub Desktop.
Group Anagrams
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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