题目:
题解:
思路:木桶效应即一只水桶能装多少谁取决于它最短的那块木板。对应题目中的text中’a’ ‘b’ ‘ll’ ‘oo’ 'n’的数量的最小值,所以遍历下字符串计数,然会返回最小值即可。
代码如下:
class Solution {
public:
// 木桶效应:一只水桶能装多少水取决于最短那块木板,对应本题中的'a' 'b' 'll' 'oo' 'n'的数量的最小值
int maxNumberOfBalloons(string text) {
vector<int> cnt(5,0);
for(char ch:text){
if(ch=='a')cnt[0]++;
else if(ch=='b')cnt[1]++;
else if(ch=='l')cnt[2]++;
else if(ch=='o')cnt[3]++;
else if(ch=='n')cnt[4]++;
}
// 组合 ll oo 的个数删一半
cnt[2]/=2,cnt[3]/=2;
return *min_element(cnt.begin(),cnt.end());
}
};