forked from PacktPublishing/Java-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrings.java
More file actions
100 lines (78 loc) · 3.12 KB
/
Copy pathStrings.java
File metadata and controls
100 lines (78 loc) · 3.12 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package modern.challenge;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import static java.util.Map.Entry.comparingByValue;
import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.groupingBy;
public final class Strings {
private static final int EXTENDED_ASCII_CODES = 256;
private Strings() {
throw new AssertionError("Cannot be instantiated");
}
public static Pair<Character, Integer> maxOccurenceCharacterV1(String str) {
if (str == null || str.isBlank()) {
// or throw IllegalArgumentException
return Pair.of(Character.MIN_VALUE, -1);
}
Map<Character, Integer> counter = new HashMap<>();
char[] chStr = str.toCharArray();
for (int i = 0; i < chStr.length; i++) {
char currentCh = chStr[i];
if (!Character.isWhitespace(currentCh)) { // ignore spaces
Integer noCh = counter.get(currentCh);
if (noCh == null) {
counter.put(currentCh, 1);
} else {
counter.put(currentCh, ++noCh);
}
}
}
int maxOccurrences = Collections.max(counter.values());
char maxCharacter = Character.MIN_VALUE;
for (Entry<Character, Integer> entry : counter.entrySet()) {
if (entry.getValue() == maxOccurrences) {
maxCharacter = entry.getKey();
}
}
return Pair.of(maxCharacter, maxOccurrences);
}
public static Pair<Character, Integer> maxOccurenceCharacterV2(String str) {
if (str == null || str.isBlank()) {
// or throw IllegalArgumentException
return Pair.of(Character.MIN_VALUE, -1);
}
int maxOccurrences = -1;
char maxCharacter = Character.MIN_VALUE;
char[] chStr = str.toCharArray();
int[] asciiCodes = new int[EXTENDED_ASCII_CODES];
for (int i = 0; i < chStr.length; i++) {
char currentCh = chStr[i];
if (!Character.isWhitespace(currentCh)) { // ignoring space
int code = (int) currentCh;
asciiCodes[code]++;
if (asciiCodes[code] > maxOccurrences) {
maxOccurrences = asciiCodes[code];
maxCharacter = currentCh;
}
}
}
return Pair.of(maxCharacter, maxOccurrences);
}
public static Pair<Character, Long> maxOccurenceCharacterV3(String str) {
if (str == null || str.isBlank()) {
// or throw IllegalArgumentException
return Pair.of(Character.MIN_VALUE, -1L);
}
return str.chars()
.filter(c -> Character.isWhitespace(c) == false) // ignoring space
.mapToObj(c -> (char) c)
.collect(groupingBy(c -> c, counting()))
.entrySet()
.stream()
.max(comparingByValue())
.map(p -> Pair.of(p.getKey(), p.getValue()))
.orElse(Pair.of(Character.MIN_VALUE, -1L));
}
}