forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1189-maximum-number-of-balloons.java
More file actions
33 lines (27 loc) · 974 Bytes
/
1189-maximum-number-of-balloons.java
File metadata and controls
33 lines (27 loc) · 974 Bytes
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
class Solution {
public int maxNumberOfBalloons(String text) {
HashMap<Character, Integer> balloon = new HashMap<>();
HashMap<Character, Integer> countText = new HashMap<>();
char[] balloonArray = "balloon".toCharArray();
for (char c : balloonArray) {
if (balloon.containsKey(c)) {
balloon.put(c,balloon.get(c)+1);
} else {
balloon.put(c,1);
}
}
char[] countTextArray = text.toCharArray();
for (char c : countTextArray) {
if (countText.containsKey(c)) {
countText.put(c,countText.get(c)+1);
} else {
countText.put(c,1);
}
}
int res = text.length();
for (Character c : balloon.keySet()) {
res = Math.min(res,countText.getOrDefault(c,0) / balloon.get(c));
}
return res;
}
}