问题描述:

给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串ransom能不能由第二个字符串magazines里面的字符构成。如果可以构成,返回 true ;否则返回 false。

(题目说明:为了不暴露赎金信字迹,要从杂志上搜索各个需要的字母,组成单词来表达意思。)

注意:

你可以假设两个字符串均只含有小写字母。

canConstruct("a", "b") -> false
canConstruct("aa", "ab") -> false
canConstruct("aa", "aab") -> true

方法:

 from collections import Counter
class Solution(object):
def canConstruct(self, ransomNote, magazine):
"""
:type ransomNote: str
:type magazine: str
:rtype: bool
"""
dic1 = Counter(ransomNote)
dic2 = Counter(magazine)
#dic1,dic2 = map(Counter,(ransomNote,magazine)
for key in dic1.keys():
if key in dic2 and dic2[key] <dic1[key] or key not in dic2:
return False
return True

官方:set(ransomNote) 建立dic存放ransomNote词频计数,用magazine.count(val) 和 dic中的val做对比

 class Solution(object):
def canConstruct(self, ransomNote, magazine):
"""
:type ransomNote: str
:type magazine: str
:rtype: bool
"""
x=set(i for i in ransomNote)
dic={}
for i in x:
dic[i]=ransomNote.count(i)
for k,v in dic.items():
if magazine.count(k)<v:
return False
return True

2018-09-28 15:55:36

LeetCode--383--赎金信的更多相关文章

  1. Java实现 LeetCode 383 赎金信

    383. 赎金信 给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串ransom能不能由第二个字符串magazines里面的字符构成.如果可以构成,返回 t ...

  2. leetcode.383赎金信

    给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串ransom能不能由第二个字符串magazines里面的字符构成.如果可以构成,返回 true :否则返回 ...

  3. 代码随想录第七天| 454.四数相加II、383. 赎金信 、15. 三数之和 、18. 四数之和

    第一题454.四数相加II 给你四个整数数组 nums1.nums2.nums3 和 nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足: 0 <= i, ...

  4. Leecode刷题之旅-C语言/python-383赎金信

    /* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...

  5. 【算法训练营day7】LeetCode454. 四数相加II LeetCode383. 赎金信 LeetCode15. 三数之和 LeetCode18. 四数之和

    [算法训练营day7]LeetCode454. 四数相加II LeetCode383. 赎金信 LeetCode15. 三数之和 LeetCode18. 四数之和 LeetCode454. 四数相加I ...

  6. 383 Ransom Note 赎金信

    给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串ransom能不能由第二个字符串magazines里面的字符构成.如果可以构成,返回 true :否则返回 ...

  7. LeetCode随缘刷题之赎金信

    欢迎评论区讨论. package leetcode.day_12_04; /** * 为了不在赎金信中暴露字迹,从杂志上搜索各个需要的字母,组成单词来表达意思. * * 给你一个赎金信 (ransom ...

  8. 【leetcode 简单】 第八十九题 赎金信

    给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串ransom能不能由第二个字符串magazines里面的字符构成.如果可以构成,返回 true :否则返回 ...

  9. [Swift]LeetCode383. 赎金信 | Ransom Note

    Given an arbitrary ransom note string and another string containing letters from all the magazines, ...

  10. LeetCode383. 赎金信

    题目 给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串 ransom 能不能由第二个字符串 magazines 里面的字符构成.如果可以构成,返回 tru ...

随机推荐

  1. JUnit Parametrized Tests

    Junit allows us to create parametrized tests. Parametrized test class has to be annotated with the @ ...

  2. freeswitch 获取app和api帮助

    通过show显示帮助命令 输出xml格式:show calls as xml 输出json格式 列出所有:show codec 解释: codec - 列出所有编码 endpoint - 列出所有en ...

  3. Leetcode66-Plus One-Eassy

    Given a non-empty array of digits representing a non-negative integer, plus one to the integer. The ...

  4. 前端UI框架总结

    H-ui 前端框架 架起设计与后端的桥梁轻量级前端框架,简单免费,兼容性好,服务中国网站. 官网:http://www.h-ui.net/H-ui.admin.shtml github下载:https ...

  5. Windows系统零开始前端开发环境配置

    1. 安装nodejs 国内下载页面(推荐) 官网下载页面 现在的nodejs自带NPM,只需点击下一步下一步安装即可. 为了加速国内NPM包下载,可配置淘宝NPM镜像 2. 安装git 国内下载页面 ...

  6. 如何终止线程的运行(C/C++)

    想要终止线程的运行,可以使用以下方法: 1.线程函数返回(最好使用该方法). 2.通过调用ExitThread函数,线程将自行撤消(最好不使用该方法). 3.同一个进程或另一个进程中的线程调用Term ...

  7. C++通过jsoncpp类库读写JSON文件-json用法详解

    介绍: JSON 是常用的数据的一种格式,各个语言或多或少都会用的JSON格式. JSON是一个轻量级的数据定义格式,比起XML易学易用,而扩展功能不比XML差多少,用之进行数据交换是一个很好的选择. ...

  8. 【Ruby】【遇到的问题】

    1 Error fetching https://gems.ruby-china.org/: certificate verify failed (https://gems.ruby-china.or ...

  9. 理解 Redis(4) - 关于 string 的更多命令(SETEX, SETNX, INCR, DECR, MSET...)

    上一节介绍了关于字符串值的一些基本命令, 这一节将介绍一些进阶命令: 清理终端: 127.0.0.1:6379> clear 设置一个键值对, 同时设置过期时间为10秒: 127.0.0.1:6 ...

  10. 【三】php 数组

    数组 1.数字索引数组:array('a','b','c');  2.访问数组内容 $arr[下标] 3.新增数组元素 $arr[下标]=内容 4.使用循环访问数组 //针对数字索引 $arr=arr ...