ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

[LeetCode] 1189. Maximum Number of Balloons

2021-09-15 02:05:08  阅读:206  来源: 互联网

标签:map min int text Number put getOrDefault Balloons LeetCode


Given a string text, you want to use the characters of text to form as many instances of the word "balloon" as possible.

You can use each character in text at most once. Return the maximum number of instances that can be formed.

Example 1:

Input: text = "nlaebolko"
Output: 1

Example 2:

Input: text = "loonbalxballpoon"
Output: 2

Example 3:

Input: text = "leetcode"
Output: 0

Constraints:

  • 1 <= text.length <= 104
  • text consists of lower case English letters only.

“气球” 的最大数量。

给你一个字符串 text,你需要使用 text 中的字母来拼凑尽可能多的单词 "balloon"(气球)。

字符串 text 中的每个字母最多只能被使用一次。请你返回最多可以拼凑出多少个单词 "balloon"。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/maximum-number-of-balloons
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这道题的思路不难,就是用 hashmap 统计。这里我给出两种实现,思路都是统计每个字母的出现次数。

时间O(n)

空间O(n)

Java实现一

 1 class Solution {
 2     public int maxNumberOfBalloons(String text) {
 3         HashMap<Character, Integer> map = new HashMap<>();
 4         for (char c : text.toCharArray()) {
 5             map.put(c, map.getOrDefault(c, 0) + 1);
 6         }
 7         
 8         int count = 0;
 9         while (true) {
10             int countB = map.getOrDefault('b', 0);
11             int countA = map.getOrDefault('a', 0);
12             int countL = map.getOrDefault('l', 0);
13             int countO = map.getOrDefault('o', 0);
14             int countN = map.getOrDefault('n', 0);
15             if (countB >= 1 && countA >= 1 && countL >= 2 && countO >= 2 && countN >= 1) {
16                 count++;
17                 map.put('b', countB - 1);
18                 map.put('a', countA - 1);
19                 map.put('l', countL - 2);
20                 map.put('o', countO - 2);
21                 map.put('n', countN - 1);
22             } else {
23                 break;
24             }
25         }
26         return count;
27     }
28 }

 

Java实现二

 1 class Solution {
 2     public int maxNumberOfBalloons(String text) {
 3         int[] map = new int[26];
 4         for (char c : text.toCharArray()) {
 5             map[c - 'a']++;
 6         }
 7         int min = map[1];                        // for b
 8         min = Math.min(min, map[0]);            // for a
 9         min = Math.min(min, map[11] / 2);        // for l /2 
10         min = Math.min(min, map[14] / 2);        // similarly for o/2
11         min = Math.min(min, map[13]);            // for n
12         return min;        
13     }
14 }

 

LeetCode 题目总结

标签:map,min,int,text,Number,put,getOrDefault,Balloons,LeetCode
来源: https://www.cnblogs.com/cnoodle/p/15270447.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有