feat: 49_group_anagrams
This commit is contained in:
parent
25a900bba3
commit
ad3c0eef54
28
49_group_anagrams/main.cpp
Normal file
28
49_group_anagrams/main.cpp
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<vector<string>> groupAnagrams(vector<string>& strs) {
|
||||||
|
unordered_map<string, vector<string>> anagrams;
|
||||||
|
|
||||||
|
for (auto& str : strs) {
|
||||||
|
string originalStr(str);
|
||||||
|
sort(str.begin(), str.end());
|
||||||
|
|
||||||
|
auto iter = anagrams.find(str);
|
||||||
|
if (iter == anagrams.end()) {
|
||||||
|
anagrams.insert({str, {originalStr}});
|
||||||
|
} else {
|
||||||
|
iter->second.emplace_back(originalStr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<vector<string>> result;
|
||||||
|
for (auto& [key, value] : anagrams) {
|
||||||
|
result.emplace_back(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user