Repository navigation
Expand file tree
/
Copy pathGroupAnagram.java
More file actions
54 lines (47 loc) · 1.68 KB
/
Copy pathGroupAnagram.java
File metadata and controls
54 lines (47 loc) · 1.68 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
48
49
50
51
52
53
54
import java.util.*;
class Solution {
// Given an array of strings strs, group the anagrams together. An anagram is a
// word formed by rearranging the letters of another word.
//
// Example:
// Input: strs = ["eat","tea","tan","ate","nat","bat"]
// Output: [["bat"],["nat","tan"],["ate","eat","tea"]]
// Explanation: Words with the same characters (sorted) belong to the same
// group.
public static List<List<String>> groupAnagrams(List<String> strs) {
HashMap<String, List<String>> hashMap = new HashMap<>();
for (String str : strs) {
char[] charArray = str.toCharArray();
Arrays.sort(charArray);
String sorted = new String(charArray);
hashMap.computeIfAbsent(sorted, k -> new ArrayList<>()).add(str);
}
return new ArrayList<>(hashMap.values());
}
public static List<String> splitWords(String s) {
return s.isEmpty() ? List.of() : Arrays.asList(s.split(" "));
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<String> strs = splitWords(scanner.nextLine());
scanner.close();
List<List<String>> res = groupAnagrams(strs);
List<List<String>> resSorted = new ArrayList<>();
for (List<String> row : res) {
List<String> copy = new ArrayList<>(row);
copy.sort(Comparator.naturalOrder());
resSorted.add(copy);
}
resSorted.sort((l1, l2) -> {
for (int i = 0; i < Math.min(l1.size(), l2.size()); i++) {
if (!l1.get(i).equals(l2.get(i))) {
return l1.get(i).compareTo(l2.get(i));
}
}
return l1.size() - l2.size();
});
for (List<String> row : resSorted) {
System.out.println(String.join(" ", row));
}
}
}