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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. leetcode 383. Ransom Note

    
Given
 an 
arbitrary
 ransom
 note
 string 
and 
another 
string 
containing 
letters from
 all 
th ...

  7. 14. leetcode 383. Ransom Note

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

  8. Java [Leetcode 383]Ransom Note

    题目描述: Given
 an 
arbitrary
 ransom
 note
 string 
and 
another 
string 
containing 
letters from
 al ...

  9. LeetCode: 383 Ransom Note(easy)

    题目: Given an arbitrary ransom note string and another string containing letters from all the magazin ...

随机推荐

  1. Delphi XE开发 Android 开机自动启动

    https://blog.csdn.net/tanqth/article/details/74357209 Android 下的广播 在Android下,要让我们开发的APP能在开机时自动启动,必须使 ...

  2. liunx trac 插件使用之GanttCalendarPlugin

    http://trac-hacks.org/wiki/GanttCalendarPlugin官网上的说明很清楚,处理做几点提示,以做记录. 1.我的Trac版本是1.0.1 我使用了'B' Metho ...

  3. window下node更新

    打开cmd查看你之前node版本安装的路径,where node: 直接去官网下载与你电脑系统(32位还是64位)对应的最新的mis版本,安装在上述路径中覆盖即可. 注意:windows上并不支持n模 ...

  4. sencha touch Carousel 自动切换

    代码是在网上找的,忘记原出处了 /** * 跑马灯自动切换 */ Ext.define('ux.RotatingCarousel', { extend: 'Ext.carousel.Carousel' ...

  5. 黄金票据(Golden Ticket)的原理与实践

    0.黄金票据是什么? 在与认证过程中,经过client与AS的通信会得到TGT,带着TGT想TGS请求,得到票据ticket,用这个ticket可以来访问应用服务器.如果这段有什么疑问,欢迎参考Ker ...

  6. 删除个别主机的Know_hosts文件信息

    方法一: rm -rf ~/.ssh/known_hosts 缺点:把其他正确的公钥信息也删除,下次链接要全部重新经过认证 方法二: vi ~/.ssh/known_hosts 删除对应ip的相关rs ...

  7. 更新jenkins插件,报错 Perhaps you need to run your container with "-Djava.awt.headless=true"?

    Configuring the Java environment variables vi ~/.bash_profile 在最后一行加入: export JAVA_OPTS=-Djava.awt.h ...

  8. OPENQUERY (Transact-SQL),跨数据库操作。

    在指定的链接服务器上执行指定的传递查询. 该服务器是 OLE DB 数据源. OPENQUERY 可以在查询的 FROM 子句中引用,就好象它是一个表名.OPENQUERY 也可以作为 INSERT. ...

  9. 【JSP】JSP中的Java脚本

    前言 现代Web开发中,在JSP中嵌入Java脚本不是推荐的做法,因为这样 不利于代码的维护.有很多好的,替代的方法避免在JSP中写Java脚本.本文仅做为JSP体系技术的一个了解.     类成员定 ...

  10. [SharePoint 2010] SharePoint 2010 部署、收回和删除解决方案----STSADM和PowerShell

    STSADM stsadm -o addsolution –filename c:\bin\CustomerSiteSearch.wsp stsadm -o deploysolution –name ...