题目如下:

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. Prometheus告警模型分析

    Prometheus作为时下最为流行的开源监控系统,其庞大的生态体系:包括针对各种传统应用的Exporter,完整的二次开发工具链,与Kubernetes等主流平台的高度亲和以及由此带来的强大的自发现 ...

  2. Asp.Net Webform 常用代码

    1015.ASP.Net WebForm 数据绑定 < %# %>   < %= %>   < % %>  < %@ %> Eval("Nam ...

  3. 卸载openssl后yum无法使用,ssh无法连接的解决办法

    一,安装nginx时,提示openssl版本问题,用yum update openssl 发现还是不行,于是rpm -e 卸载了openssl 此时,才发现yum不能用了,然后想上传个tar包时又出现 ...

  4. 【HANA系列】SAP HANA计算视图中的RANK使用方法

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA计算视图中的RA ...

  5. cocos2dx基础篇(2) 第一个程序

    [本节内容] 1.程序的基本组成:CCSprite(精灵).CCLayer(层).CCScene(场景).CCDirector(导演) 2.分析HelloWorld源码. 一.基本组成 cocos2d ...

  6. USACO4.1 Fence Loops【最小环&边->点转化】

    数据不是很大,如果要转换为正常的那种建图方式的话,可以给点进行标号,用一个二维数组存这两条边相交的那个点的标号,方便处理.一定要注意不要同一个点使用不同的编号也不要不同的点使用同一个编号(这不是废话嘛 ...

  7. 拉格朗日乘法与KKT条件

    问题的引出 给定一个函数\(f\),以及一堆约束函数\(g_1,g_2,...,g_m\)和\(h_1,h_2,...,h_l\).带约束的优化问题可以表示为 \[ \min_{X \in R^n}f ...

  8. selenium学习-模拟键盘按键操作

    导入  from selenium.webdriver.common.keys import Keys  格式:Keys.XXX 一般这么用:send_keys(Keys.XXX) # coding= ...

  9. 实例学习——爬取Pexels高清图片

    近来学习爬取Pexels图片时,发现书上代码会抛出ConnectionError,经查阅资料知,可能是向网页申请过于频繁被禁,可使用time.sleep(),减缓爬取速度,但考虑到爬取数据较多,运行时 ...

  10. Python 入门之 内置模块 -- datetime模块

    Python 入门之 内置模块 -- datetime模块 1.datetime模块 from datetime import datetime (1)datetime.now() 获取当前时间和日期 ...