-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
40 lines (34 loc) · 909 Bytes
/
main.cpp
File metadata and controls
40 lines (34 loc) · 909 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
vector<vector<string>> groupAnagrams(vector<string>& strs)
{
unordered_map<string, vector<string>> anagram_groups;
for (string word : strs)
{
string sorted_word = word;
sort(sorted_word.begin(), sorted_word.end());
anagram_groups[sorted_word].push_back(word);
}
vector<vector<string>> answer;
for (auto& group : anagram_groups)
answer.push_back(group.second);
return answer;
}
};
int main()
{
vector<string> input = {"eat","tea","tan","ate","nat","bat"};
vector<vector<string>> answer = Solution().groupAnagrams(input);
for (vector<string> array: answer)
{
cout << "[";
for (string s: array)
cout << s << " ";
cout << "] ";
}
cout << endl;
return 0;
}