题目如下:

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中b,a,l,o,n这五个字符的出现次数的最小值即可。但有两点需要注意,一是text必须同时包含这五个字符,而是l和o是要算双份。

代码如下:

class Solution(object):
def maxNumberOfBalloons(self, text):
"""
:type text: str
:rtype: int
"""
dic = {}
char_list = ['b','a','l','o','n']
res = float('inf')
for i in text:
if i not in char_list:
continue
dic[i] = dic.setdefault(i,0) + 1
if len(char_list) != len(dic):
return 0
for k,v in dic.iteritems():
if k in ('l','o'):
res = min(res,v/2)
else:
res = min(res,v)
return res

【leetcode】1189. Maximum Number of Balloons的更多相关文章

  1. 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)

    [LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...

  2. 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)

    [LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...

  3. 【leetcode】998. Maximum Binary Tree II

    题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than ...

  4. 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)

    [LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  5. 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)

    [LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...

  6. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

  7. 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)

    [LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...

  8. 【LeetCode】306. Additive Number 解题报告(Python)

    [LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  9. 【POJ2699】The Maximum Number of Strong Kings(网络流)

    Description A tournament can be represented by a complete graph in which each vertex denotes a playe ...

随机推荐

  1. HashMap根据value值排序

    )))); Set<Map.Entry<String, Integer>> entrySet = map.entrySet(); for (Map.Entry s : entr ...

  2. 求助,在gmssl中添加第三方库engine的问题

    求助gmssl的一个问题,想知道gmssl怎么添加自己的硬件engine,有搞过的大佬吗,求助.我现在将第三方的sdf标准库,在gmssl的源码中通过gmssl engine尝试添加总是报错libsd ...

  3. eclipse 包含头文件出错

    最近这段时间自己在写游戏的框架,在做的过程中遇到了一个问题:没办法#include <iostream>,在eclipse下F3也找不到对应的 iostream 文件, 最后在晚上找资料, ...

  4. centos7.5 升级kernel内核版本

    一,查看当前系统内核版本信息 awk -F\' '$1=="menuentry " {print i++ " : " $2}' /etc/grub2.cfg 或 ...

  5. Unity旋转问题的总结

    1.物体的直接旋转 transform.Rotate();这个函数是在当前状态下网某个方向旋转.并且这里可以设置为世界空间或者自身空间. transform.rotation;这里可以通过直接定义一个 ...

  6. 磁盘的分区和挂载(mount)

    一.挂载问题的引入 我们大多数人用惯了windos系统,对linux系统中磁盘的管理就先入为主,不太好理解挂载这一动作.在linux系统中添加一块新磁盘后,要进行分区.格式化(分配文件系统).挂载.当 ...

  7. 应用安全 - 工具 - 浏览器 - 火狐(FireFox) - 漏洞汇总

    CVE-2010-3131   Date Aug 类型 Mozilla Firefox - 'dwmapi.dll' DLL Hijacking 影响范围 Firefox <= CVE-2010 ...

  8. win10 hhctrl.ocx 丢失

    1.我的是从同事电脑上复制过来的,他电脑也是win102.复制文件“hhctrl.ocx”到系统目录下  32位系统目录为:C:\WINNT\System32:64位系统为C:\Windows\Sys ...

  9. Mac OS X 11中的/usr/bin 的“Operation not permitted”

    更新了 Mac OS X 11后发现,MacVim 不再能够通过Terminal用命令打开了. mvim hello.txt 于是尝试将 mvim 重新复制到/usr/bin/中去 sudo cp - ...

  10. HDU 1297 Children’s Queue (递推、大数相加)

    Children’s Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...