File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ HashMap <String , List <String >> map = new HashMap <>();
3
+
4
+ public List <List <String >> groupAnagrams (String [] strs ) {
5
+
6
+ for (int idx =0 ;idx <strs .length ;idx ++){
7
+ process (strs [idx ]);
8
+ }
9
+
10
+ return new ArrayList <List <String >>(map .values ());
11
+ }
12
+
13
+ public void process (String data ){
14
+ int [] az = new int [26 ];
15
+
16
+ for (int idx =0 ;idx <data .length ();idx ++){
17
+ az [data .charAt (idx )-'a' ] += 1 ;
18
+ }
19
+ StringBuilder result = new StringBuilder ();
20
+ for (int idx =0 ;idx <26 ;idx ++){
21
+ if (az [idx ] == 0 ) continue ;
22
+
23
+ result .append (idx +"-" +az [idx ]);
24
+ }
25
+ String out = result .toString ();
26
+ if (!map .containsKey (out )) map .put (out , new ArrayList ());
27
+
28
+ map .get (out ).add (data );
29
+ }
30
+ }
You can’t perform that action at this time.
0 commit comments