-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGroupAnagrams.java
45 lines (38 loc) · 1011 Bytes
/
GroupAnagrams.java
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
package ProjectFiles;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
public class GroupAnagrams {
public static void main(String[] args) {
List<List<String>> list = groupAnagrams(new String[]{"eat", "tea", "tan", "ate", "nat", "bat"});
for(List<String > l : list){
System.out.println(l);
}
}
public static List<List<String>> groupAnagrams(String[] strs) {
HashMap<String, List<String>> map = new HashMap<String, List<String>>();
for(String s: strs){
char[] temp = s.toCharArray();
Arrays.sort(temp);
String key = new String(temp);
List<String> list;
if(map.containsKey(key)){
list = map.get(key);
list.add(s);
}
else{
list = new ArrayList<String>();
list.add(s);
map.put(key, list);
}
}
List<List<String>> result = new ArrayList<List<String>>();
for(List<String> e : map.values()){
Collections.sort(e);
result.add(e);
}
return result;
}
}