给你一个字符串 text,你需要使用 text 中的字母来拼凑尽可能多的单词 "balloon"(气球)。
字符串 text 中的每个字母最多只能被使用一次。请你返回最多可以拼凑出多少个单词 "balloon"。
示例 1:
输入:text = "nlaebolko"
输出:1
示例 2:
输入:text = "loonbalxballpoon"
输出:2
示例 3:
输入:text = "leetcode"
输出:0
提示:
1 <= text.length <= 10^4
text 全部由小写英文字母组成
#include<iostream>
#include<unordered_map>
#include<unordered_set>
using namespace std;
class Solution {
public:
int maxNumberOfBalloons(string text) {
int minval = INT_MAX;
std::string str = "balloon";
unordered_set<char> filter; /* 记录是否存取全部 */
unordered_map<char, int>::iterator it;
unordered_map<char, int> cur;
unordered_map<char, int> mp;
mp['b'] = 1;
mp['a'] = 1;
mp['l'] = 2;
mp['o'] = 2;
mp['n'] = 1;
for (char c : text) {
if (cur.count(c) == 0) {
cur[c] = 1;
}
else {
cur[c]++;
}
}
it = cur.begin();
while (it != cur.end()) {
if (mp.count(it->first) != 0) {
filter.insert(it->first);
if ((cur[it->first] / mp[it->first]) < minval) {
minval = cur[it->first] / mp[it->first];
}
}
it++;
}
if (filter.size() != 5) {
minval = 0;
}
return minval;
}
};