forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnagramsTogetherLexicographically.java
75 lines (63 loc) · 2.42 KB
/
AnagramsTogetherLexicographically.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.rampatra.strings;
import java.util.*;
/**
* Created by IntelliJ IDEA.
*
* @author rampatra
* @since 10/11/15
*/
public class AnagramsTogetherLexicographically {
/**
* Takes an array of String {@code strings} and prints anagrams in groups where the groups
* are arranged lexicographically and the strings within each group are also arranged
* lexicographically.
*
* @param strings
*/
public static void printAnagramsTogether(String[] strings) {
HashMap<String, List<Integer>> hashMap = new HashMap<>();
TreeSet<List<String>> treeSet = new TreeSet<>((Comparator) (o1, o2) -> {
if (o1 instanceof List<?> && o2 instanceof List<?>) {
return ((List<String>) o1).get(0).compareTo(((List<String>) o2).get(0));
} else {
return 0;
}
});
for (int i = 0; i < strings.length; i++) {
String spaceRemovedStr = strings[i].replaceAll("\\s+", "");
char[] chars = spaceRemovedStr.toCharArray();
Arrays.sort(chars);
List<Integer> indexes = hashMap.get(String.valueOf(chars));
if (indexes == null) {
indexes = new ArrayList<>();
}
indexes.add(i);
hashMap.put(String.valueOf(chars), indexes);
}
for (Map.Entry<String, List<Integer>> entry : hashMap.entrySet()) {
List<String> anagrams = new ArrayList<>();
for (int i = 0; i < entry.getValue().size(); i++) {
anagrams.add(strings[entry.getValue().get(i)]);
}
Collections.sort(anagrams); // arrange anagrams lexicographically within a single line
treeSet.add(anagrams); // sort the entire output lexicographically
}
treeSet.stream().flatMap(Collection::stream).forEach(System.out::println);
}
/**
* Take list of strings from console and print anagrams in groups.
*
* @param args
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
List<String> strings = new ArrayList<>();
String s;
System.out.println("Input string in separate lines (blank string to stop):");
// you should use in.hasNextLine()
while (!(s = in.nextLine()).trim().equals("")) {
strings.add(s);
}
printAnagramsTogether(strings.toArray(new String[0]));
}
}