​https://leetcode-cn.com/problems/maximum-number-of-balloons/​

1189. “气球” 的最大数量_算法

这道题,采用统计的方法,
木桶效应,一只水桶能装的水,取决于最短的那块木板

class Solution {
public int maxNumberOfBalloons(String text) {
int[] cnts = new int[5];
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (c == 'b') cnts[0]++;
else if (c == 'a') cnts[1]++;
else if (c == 'l') cnts[2]++;
else if (c == 'o') cnts[3]++;
else if (c == 'n') cnts[4]++;
}
cnts[2] /= 2; cnts[3] /= 2;
int ans = cnts[0];
//注意min的用法
for (int i = 0; i < 5; i++) ans = Math.min(ans, cnts[i]);
return ans;
}
}

Java charAt() 方法