-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathGroupAnagram.java
More file actions
28 lines (26 loc) · 821 Bytes
/
GroupAnagram.java
File metadata and controls
28 lines (26 loc) · 821 Bytes
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
#From an array, need to filterout all the strings that are anagrams
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class GroupAnagram
{
public static void main(String[] args)
{
String array[] = {"eat", "god", "tea", "dog", "ate"};
Map<String, List<String>> map = new HashMap<String, List<String>>();
for (int i = 0; i < array.length; i++)
{
char[] ch = array[i].toCharArray();
Arrays.parallelSort(ch);
String sorted = new String(ch);
if(!map.containsKey(sorted))
{
map.put(sorted, new LinkedList<String>());
}
map.get(sorted).add(array[i]);
}
System.out.println(map);
}
}