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
from collections import Counter
class Solution(object):
def canConstruct(self, ransomNote, magazine):
"""
:type ransomNote: str
:type magazine: str
:rtype: bool
"""
c=Counter(magazine)
for i in ransomNote:
if c[i]==0 or i not in c:
return False
else:
c[i]-=1
return True

  

[LeetCode&Python] Problem 383. Ransom Note的更多相关文章

  1. 383. Ransom Note【easy】

    383. Ransom Note[easy] Given an arbitrary ransom note string and another string containing letters f ...

  2. 【LeetCode】383. Ransom Note 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...

  3. leetcode 383. Ransom Note

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

  4. leetcode修炼之路——383. Ransom Note

    题目是这样的 Given
 an 
arbitrary
 ransom
 note
 string 
and 
another 
string 
containing 
letters from
 a ...

  5. 14. leetcode 383. Ransom Note

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

  6. Java [Leetcode 383]Ransom Note

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

  7. LeetCode: 383 Ransom Note(easy)

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

  8. 383. Ransom Note

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

  9. 383. Ransom Note 在字典数组中查找笔记数组

    [抄题]: Given an arbitrary ransom note string and another string containing letters from all the magaz ...

随机推荐

  1. Python print打印

    1.Python运算符: +:加 -:减 *:乘 /:除以 %:去除法后的余数 //:取整除 2.字符串center方法: a='111'print(a.center(4,'2')) #让字符串占位 ...

  2. ubuntu 双硬盘挂载 windows分区自动挂载

    sudo fdisk -l 查看硬盘情况 1:新建一个目录,例:old 2:mount  /dev/sdb1  old 3:cd old 4:ls  (就可以看到新硬盘的内容了) 取消挂载:umoun ...

  3. Cyclic Components CodeForces - 977E(DFS)

    Cyclic Components CodeForces - 977E You are given an undirected graph consisting of nn vertices and  ...

  4. laravel处理菜单保持的方法:

    <!-- 左侧菜单区域 --> <div class="col-md-3"> @section('leftmenu') <div class=&quo ...

  5. learning ddr3 protocol

    refercece:  www.jedec.org https://www.cnblogs.com/zhongguo135/p/8486979.html : 

  6. java字符串根据空格截取并存进ArrayList,并在每个元素前后加上/

    public class List { public static void main(String[] args) { String s = "abc nnn ooo/xzsxc bs&q ...

  7. eclipse.ini参数配置

    -vmD:/jdk1.6/Java/jdk1.6.0_45/bin/javaw.exe-vmargs-Xms1024m-Xmx1024m-XX:MaxPermSize=1024m-XX:Reserve ...

  8. 【资料搜集】DirectX学习

    [网站推荐:]GameRes游资网-游戏开发者门户 http://www.gameres.com/ [基础知识:] <游戏编程>第一部 基础篇 - GameRes.com http://d ...

  9. sublime text3 常用配置

    . 打开Preferences菜单,并选择 Browse Packages… . 系统会打开Sublime Text 的Packages文件夹,回到上一级菜单,然后打开Installed Packag ...

  10. 第二节 java流程控制(循环结构)

     1.for循环 for(初始化表达式;循环条件表达式;循环后的操作表达式){ 执行语句 } 2.while循环 while(条件表达式){ 执行语句 } while循环特点是只有条件满足才会执行我们 ...