方法一:
switch case:
class Solution {
public:
int maxNumberOfBalloons(string text) {
int res[5]={0};
for(auto i:text){
switch (i){
case'b':res[0]++;break;
case'a':res[1]++;break;
case'l':res[2]++;break;
case'o':res[3]++;break;
case'n':res[4]++;break;
}
}
res[2]/=2;
res[3]/=2;
int cnt=INT_MAX;
for(int i=0;i<5;i++){
cnt=min(cnt,res[i]);
}
return cnt;
}
};
方法二:
hashmap:
class Solution {
public:
int maxNumberOfBalloons(string text) {
unordered_map<char,int> hash;
for(int i=0;i<text.size();i++){
hash[text[i]]++;
}
return min(min(min(min(hash['b'],hash['a']),hash['l']/2),hash['o']/2),hash['n']);
}
};