-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprint_anagram.cpp
59 lines (48 loc) · 1.11 KB
/
print_anagram.cpp
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
class Solution
{
public:
vector<vector<string> > Anagrams(vector<string>& string_list)
{
map<string, vector<string>>m;
for(int i=0;i<string_list.size();i++)
{
string s=string_list[i];
sort(s.begin(), s.end());
m[s].push_back(string_list[i]);
}
vector<vector<string> > v;
for(auto i:m)
{
v.push_back(i.second);
}
return v;
}
};
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
vector<string> string_list(n);
for (int i = 0; i < n; ++i)
cin>>string_list[i];
Solution ob;
vector<vector<string> > result = ob.Anagrams(string_list);
sort(result.begin(),result.end());
for (int i = 0; i < result.size(); i++)
{
for(int j=0; j < result[i].size(); j++)
{
cout<<result[i][j]<<" ";
}
cout<<"\n";
}
}
return 0;
}