问题描述
给你一个字符串 text,你需要使用 text 中的字母来拼凑尽可能多的单词 "balloon"(气球)。
字符串 text 中的每个字母最多只能被使用一次。请你返回最多可以拼凑出多少个单词 "balloon"。
示例 1:
输入:text = "nlaebolko"
输出:1
示例 2:
输入:text = "loonbalxballpoon"
输出:2
示例 3:
输入:text = "leetcode"
输出:0
提示:
1 <= text.length <= 10^4
text
全部由小写英文字母组成
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/maximum-number-of-balloons
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
执行结果
代码描述
思路:开辟一个26位的数组,下标表示字符,值表示个数。根据单词的需要,直接选取最小个数,即为单词的个数。
class Solution {
public:
int maxNumberOfBalloons(string text) {
int arr[26] = {0};
for(int i = 0; i < text.size(); ++i)
{
arr[text[i]-'a']++;
}
int count = 0;
//a=0, b=1, l=11, n=13, o=14,
count = min(arr[0], arr[1]);
count = min(arr[11]/2, count);
count = min(arr[13], count);
count = min(arr[14]/2, count);
return count;
}
};