forked from imnishant/GeeksforGeeks-Java-Solution
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroupAnagram.java
More file actions
44 lines (43 loc) · 1.26 KB
/
GroupAnagram.java
File metadata and controls
44 lines (43 loc) · 1.26 KB
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
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static void main (String[] args) throws Exception
{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(bf.readLine());
while(t-- > 0)
{
int n = Integer.parseInt(bf.readLine());
String s[] = bf.readLine().trim().split(" ");
HashMap<String, Integer> hm = new HashMap<String, Integer>();
for(int i=0 ; i<n ; i++)
{
int arr[] = new int[26];
for(int j=0 ; j<s[i].length() ; j++)
arr[s[i].charAt(j) - 'a']++;
String res = "";
for(int j=0 ; j<26 ; j++)
res += arr[j];
if(!hm.containsKey(res))
hm.put(res, 1);
else
{
int count = hm.get(res);
hm.remove(res);
hm.put(res, count+1);
}
}
String s1 = "";
Set set = hm.entrySet();
Iterator it = set.iterator();
while(it.hasNext())
{
Map.Entry me = (Map.Entry) it.next();
s1 = me.getValue() + " " + s1;
}
System.out.println(s1);
}
}
}