-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patha. Anagrams.cpp
58 lines (52 loc) · 1.34 KB
/
a. Anagrams.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
/*
Problem Link: https://practice.geeksforgeeks.org/problems/print-anagrams-together/1/
Title: Print Anagrams Together
Difficulty: Medium
Author: Hariket Sukesh Kumar Sheth
Language: C++
*/
#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
class Solution{
public:
vector<vector<string> > Anagrams(vector<string>& string_list) {
int n = string_list.size();
map <string,vector <string>> hari;
for(int i=0; i<n; i++){
string temp = string_list[i];
sort(temp.begin(), temp.end());
hari[temp].push_back(string_list[i]);
}
vector <vector <string>> ans;
for(auto it=hari.begin(); it!=hari.end(); it++){
ans.push_back(it->second);
}
return ans;
}
};
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;
}