为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode1189. 气球的最大数量 |Maximum Number of Balloons

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(www.zengqiang.org
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/11521665.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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 <= 10^4
  • text consists of lower case English letters only.

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

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

 

示例 1:

输入:text = "nlaebolko"
输出:1

示例 2:

输入:text = "loonbalxballpoon"
输出:2

示例 3:

输入:text = "leetcode"
输出:0

 

提示:

  • 1 <= text.length <= 10^4
  • text 全部由小写英文字母组成

12ms
 1 class Solution {
 2     func maxNumberOfBalloons(_ text: String) -> Int {
 3         var textCountMap: [Character: Int] = ["b":0, 
 4                             "l":0,
 5                             "a":0,
 6                             "o":0,
 7                             "n":0]
 8         for char in text {
 9             switch char {
10                 case "b",
11                     "l",
12                     "a",
13                     "o",
14                     "n":
15                     textCountMap[char, default: 0] += 1
16                 default:
17                     continue
18             }
19         }
20         
21         if textCountMap["o"]! % 2 == 1 { 
22             textCountMap["o"] = textCountMap["o"]! - 1 
23         }
24         
25         if textCountMap["l"]! % 2 == 1 { 
26             textCountMap["l"] = textCountMap["l"]! - 1 
27         }
28         
29         return min(textCountMap["b"]!, textCountMap["n"]!, textCountMap["a"]!, textCountMap["o"]!/2, textCountMap["l"]!/2)
30     }
31 }

Runtime: 16 ms

Memory Usage: 20.7 MB
 1 class Solution {
 2     func maxNumberOfBalloons(_ text: String) -> Int {
 3         var F:[Character:Int] = [Character:Int]()
 4         for c in text
 5         {
 6             F[c,default:0] += 1
 7         }
 8         return min(F["b",default:0], F["a",default:0], F["l",default:0]/2, F["o",default:0]/2, F["n",default:0])
 9     }
10 }

 

posted @ 2019-09-15 12:05  为敢技术  阅读(439)  评论(0编辑  收藏  举报