blob: 65a8a04a6733181884a7c5d29696e14cfc408d1c (
plain)
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
|
// Same algorithm as the perl version, using a LinkedHashMap to
// preserve order of keys, and streams to avoid most loops.
import java.util.*;
class ch2 {
private static void task2(String s) {
ArrayList<Character> chars = new ArrayList<Character>();
chars.ensureCapacity(s.length());
s.chars().forEach(ch -> chars.add((char)ch));
Collections.reverse(chars);
StringBuilder sb = new StringBuilder();
while (chars.size() > 0) {
HashMap<Character, Integer> counts =
new LinkedHashMap<Character, Integer>();
chars.stream().forEach(ch -> counts.put(ch, counts.getOrDefault(ch, 0) + 1));
char fnr = counts.entrySet().stream()
.filter(entry -> entry.getValue() == 1)
.findFirst().map(entry -> entry.getKey()).orElse('#');
sb.append(fnr);
chars.remove(0);
}
sb.reverse();
System.out.println(sb);
}
public static void main(String[] args) {
task2("ababc");
task2("xyzzyx");
}
}
|