leetdoce-1189-easy
Maximum Number of Balloons
Given a string text, you want to use the characters of text to form as many instances of the word "balloon" as possible.
You can use each character in text at most once. Return the maximum number of instances that can be formed.
Example 1:
Input: text = "nlaebolko"
Output: 1
Example 2:
Input: text = "loonbalxballpoon"
Output: 2
Example 3:
Input: text = "leetcode"
Output: 0
Constraints:
1 <= text.length <= 104
text consists of lower case English letters only.
思路一:统计字符数量,注意重复的字符
public int maxNumberOfBalloons(String text) {
int[] count = new int[26];
char[] chars = text.toCharArray();
for (char c : chars) {
count[c - 'a']++;
}
String balloon = "balloon";
count['l' - 'a'] = count['l' - 'a'] / 2;
count['o' - 'a'] = count['o' - 'a'] / 2;
int result = Integer.MAX_VALUE;
for (char c : balloon.toCharArray()) {
result = Math.min(count[c - 'a'], result);
}
return result;
}

浙公网安备 33010602011771号