-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroupAnagrams.java
More file actions
47 lines (42 loc) · 1.42 KB
/
Copy pathGroupAnagrams.java
File metadata and controls
47 lines (42 loc) · 1.42 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
45
46
47
import java.util.*;
import java.util.stream.Collectors;
/*
Given an array of strings, group anagrams together.
Example:
Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
Output:
[
["ate","eat","tea"],
["nat","tan"],
["bat"]
]
Note:
All inputs will be in lowercase.
The order of your output does not matter.
https://leetcode.com/problems/group-anagrams/
*/
public class GroupAnagrams {
public static void main(String[] args) {
String[] input = new String[]{"ate", "eat", "tea", "tan", "nat", "bat"};
System.out.println(groupAnagrams(input));
}
public static List<List<String>> groupAnagrams(String[] strs) {
int[] count = new int[26];
Map<String, List<String>> ans = new HashMap<>();
Arrays.stream(strs).forEach(value -> {
char[] chars = value.toCharArray();
Arrays.fill(count, 0);
for (char ch : chars) {
count[ch - 'a'] = count[ch - 'a'] + 1; //count frequency of each character
}
StringBuilder sb = new StringBuilder();
for (int c : count) {
sb.append("#" + c + count[c]);
}
List<String> orDefault = ans.getOrDefault(sb.toString(), new ArrayList<>());
orDefault.add(value);
ans.put(sb.toString(), orDefault);
});
return ans.entrySet().parallelStream().map(Map.Entry::getValue).collect(Collectors.toList());
}
}