-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathMaximumNumberOfBalloons.java
32 lines (29 loc) · 1.21 KB
/
MaximumNumberOfBalloons.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
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class MaximumNumberOfBalloons {
private static final Set<Character> BALLOON_CHARACTERS = Set.of('b', 'a', 'l', 'o', 'n');
private static final Set<Character> BALLOON_DOUBLE_LETTERS = Set.of('l', 'o');
public int maxNumberOfBalloons(String text) {
Map<Character, Integer> frequencies = getCharacterFrequencies(text);
int result = Integer.MAX_VALUE;
for (char character : BALLOON_CHARACTERS) {
result = Math.min(result, frequencies.getOrDefault(character, 0));
}
return result;
}
private Map<Character, Integer> getCharacterFrequencies(String string) {
final Map<Character, Integer> frequencies = new HashMap<>();
char character;
for (int i = 0 ; i < string.length() ; i++) {
character = string.charAt(i);
if (BALLOON_CHARACTERS.contains(character)) {
frequencies.put(character, frequencies.getOrDefault(character, 0) + 1);
}
}
for (char letter : BALLOON_DOUBLE_LETTERS) {
frequencies.put(letter, frequencies.getOrDefault(letter, 0) / 2);
}
return frequencies;
}
}