[LeetCode] 383. Ransom Note_Easy tag: Hash Table
Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false.
Each letter in the magazine string can only be used once in your ransom note.
Note:
You may assume that both strings contain only lowercase letters.
canConstruct("a", "b") -> false
canConstruct("aa", "ab") -> false
canConstruct("aa", "aab") -> true
题目思路就是将note和magzine的字符串用Counter来计数, 针对每个note的每个字符, 如果d_m[c] > d_note[c], 就可行, 否则False
Code
class Solution:
def ransomNote(self, note, magzine):
d_note, d_mag = collections.Counter(note), collections.Counter(magzine)
for k in d_note:
if d_note[k] > d_mag[k]:
return False
return True
[LeetCode] 383. Ransom Note_Easy tag: Hash Table的更多相关文章
- [LeetCode] 1. Two Sum_Easy tag: Hash Table
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- [LeetCode] 804. Unique Morse Code Words_Easy tag: Hash Table
International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...
- [LeetCode] 884. Uncommon Words from Two Sentences_Easy tag: Hash Table
We are given two sentences A and B. (A sentence is a string of space separated words. Each word co ...
- [LeetCode] 532. K-diff Pairs in an Array_Easy tag: Hash Table
Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in t ...
- [LeetCode] 697. Degree of an Array_Easy tag: Hash Table
Given a non-empty array of non-negative integers nums, the degree of this array is defined as the ma ...
- leetcode 383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all th ...
- 14. leetcode 383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all the magazines, ...
- Java [Leetcode 383]Ransom Note
题目描述: Given an arbitrary ransom note string and another string containing letters from al ...
- LeetCode: 383 Ransom Note(easy)
题目: Given an arbitrary ransom note string and another string containing letters from all the magazin ...
随机推荐
- 纯css制作带三角(兼容所有浏览器)
如何用 border 来制作三角. html 代码如下: 代码如下: <div class="arrow-up"></div> <div class= ...
- 【Spring源码分析系列】搭建Spring实现容器的基本实现
前言 bean是Spring中最核心的东西,因为Spring就像一个大水桶,而bean就像是容器中的水,先新建一个小例子来看一下: 一.使用eclipse构建项目,项目结构如下 二.类文件内容 < ...
- 题目1010:A + B(字符串转数字)
题目链接:http://ac.jobdu.com/problem.php?pid=1010 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...
- 题目1006:ZOJ问题(递推规律)
题目链接:http://ac.jobdu.com/problem.php?pid=1006 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...
- php 函数合并 array_merge 与 + 的区别
array_merge()是PHP语言中的一个函数,作用是将两个或多个数组的单元合并起来,一个数组中的值附加在前一个数组的后面.返回作为结果的数组. 如果输入的数组中有相同的字符串键名,该键的键值为最 ...
- Python并行编程的几个要点
一.基于线程的并行编程 如何使用Python的线程模块 如何定义一个线程 如何探测一个线程 如何在一个子类中使用线程 Lock和RLock实现线程同步 信号实现线程同步 条件(condition)实现 ...
- 【CF884F】Anti-Palindromize 费用流
[CF884F]Anti-Palindromize 题意:定义一个串是反回文的,当且仅当对于1<=i<=len,$a_i!=a_{len-i+1}$. 现在给出一个长度为n的串S(n是偶数 ...
- iOS interface适配
- wpgcms---碎片管理的使用
这里很神奇的是碎片管理是编辑器,所以拿到的配置都是富文本,所以在前台作为字段来使用的时候,需要过滤掉字符串. 具体示例: {% set qq = wpg.fragment.get("qq&q ...
- 百度地图InfoWindow弹窗圆角
效果如下 使用CSS样式 /*地图标题*/ .BMap_pop div:nth-child(1) div { border-radius: 8px 0 0 0; } .BMap_pop div:nth ...