leetcode 383. Ransom Note
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
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
map<char,int> mpr,mpm;
map<char,int>::iterator itr,itm;
int i ,j;
for(i=; i < ransomNote.length();++i)
{
mpr[ransomNote[i]]++;
}
for(j = ; j<magazine.length();++j)
{
mpm[magazine[j]]++;
}
if(mpr.size() > mpm.size()) return false;
itr = mpr.begin();itm = mpm.begin();
while(itr != mpr.end() && itm != mpm.end())
{
char tmp = itr->first;
map<char,int>::iterator t; if(mpm.find(tmp) == mpm.end())
return false; if((int)(itr->second) > (int)(mpm.find(tmp)->second))
return false;
else
itr++;
}
return true; }
};
leetcode 383. Ransom Note的更多相关文章
- 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 ...
- 383. Ransom Note【easy】
383. Ransom Note[easy] Given an arbitrary ransom note string and another string containing letters f ...
- 【LeetCode】383. Ransom Note 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
- leetcode修炼之路——383. Ransom Note
题目是这样的 Given an arbitrary ransom note string and another string containing letters from a ...
- [LeetCode&Python] Problem 383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all the magazines, ...
- 383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all th ...
- [LeetCode] 383. Ransom Note_Easy tag: Hash Table
Given an arbitrary ransom note string and another string containing letters from all the magazines, ...
随机推荐
- linux连接远程桌面
#!/usr/bin/env python3 #-*-encoding:utf-8-*- import re import os import urllib.request, urllib.parse ...
- Caffe初试(三)使用caffe的cifar10网络模型训练自己的图片数据
由于我涉及一个车牌识别系统的项目,计划使用深度学习库caffe对车牌字符进行识别.刚开始接触caffe,打算先将示例中的每个网络模型都拿出来用用,当然这样暴力的使用是不会有好结果的- -||| ,所以 ...
- CentOS7 FTP服务搭建(虚拟用户访问FTP服务)
概述 最近在搞Oracle在Linux系统下集群,针对Linux系统,笔人也是一片空白.Liunx外部文件的传输,避免不了使用FTP服务,所以现在就整理下,CentOS7环境下,FTP服务的搭建.FT ...
- 对DIP IoC DI的理解与运用
DIP,IoC,DI基本概念 依赖倒置原则(DIP,Dependency Inverse Principle):强调系统的“高层组件”不应当依赖于“底层组件”,并且不论是“高层组件”还是“底层组件”都 ...
- hdu 4717(三分求极值)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4717 思路:三分时间求极小值. #include <iostream> #include ...
- MIT 6.828 JOS学习笔记9. Exercise 1.5
Lab 1 Exercise 5 再一次追踪一下boot loader的一开始的几句指令,找到第一条满足如下条件的指令处: 当我修改了boot loader的链接地址,这个指令就会出现错误. 找到这样 ...
- android 得到缩略图
转载至 http://blog.csdn.net/dxh040431104/article/details/6667448 怎样获取图片的大小?思路很简单:首先我们把这个图片转成Bitmap,然后再利 ...
- 解决SVN更新代码是出现previous operation has not finished; run cleanup if it was interrupted这个错误
解决方法:清空svn的队列 1.下载sqlite3.exe 2.找到你项目的.svn文件,查看是否存在wc.db (查看.svn文件需要打开显示隐藏文件夹) 3.将sqlite3.exe放到.sv ...
- 【转载】科研ppt制作的体会
转载自实验室陈家雷学长发在bbs 上的帖子,讲解了自己制作ppt的心得体会.学习下. 附件中是我昨天晚上我的组会ppt的pdf版本,另外我对ppt的制作有点自己的理解,基本上都是去年暑假在Harvar ...
- linux vi命令
>>>>>>>>>>>>>>>>>>>> 2016.12.6: vi是交互式的编 ...