/*
* @lc app=leetcode.cn id=383 lang=c
*
* [383] 赎金信
*
* https://leetcode-cn.com/problems/ransom-note/description/
*
* algorithms
* Easy (44.98%)
* Total Accepted: 5K
* Total Submissions: 11.1K
* Testcase Example: '"a"\n"b"'
*
* 给定一个赎金信 (ransom)
* 字符串和一个杂志(magazine)字符串,判断第一个字符串ransom能不能由第二个字符串magazines里面的字符构成。如果可以构成,返回
* true ;否则返回 false。
*
* (题目说明:为了不暴露赎金信字迹,要从杂志上搜索各个需要的字母,组成单词来表达意思。)
*
* 注意:
*
* 你可以假设两个字符串均只含有小写字母。
*
*
* canConstruct("a", "b") -> false
* canConstruct("aa", "ab") -> false
* canConstruct("aa", "aab") -> true
*
*
*/
bool canConstruct(char* ransomNote, char* magazine) {
if (ransomNote == NULL || magazine == NULL) {
return false;
}
int index[];
for(int i=;i<;i++){
index[i]=;
}
for (int i = ; i <strlen(magazine); i++) {
index[magazine[i]-'a']++;
}
for (int j = ; j <strlen(ransomNote); j++) {
if (--index[ransomNote[j]- 'a'] < ) {
return false;
}
}
return true;
}

这里的思路就是,建立一个数组,26个位置,0代表a,以此类推。

然后统计杂志中也就是magazine中所有各个字符出现的次数。

然后在杂志中出现一个字符就减掉index中的字符次数,其实相当于 买东西 和 库存 的含义。如果小于零的话就证明那个字符不够用,返回false;

这里index不初始化的话就不行。。。(不知道为什么)

-----------------------------------------------------------------------

python:

#
# @lc app=leetcode.cn id=383 lang=python3
#
# [383] 赎金信
#
# https://leetcode-cn.com/problems/ransom-note/description/
#
# algorithms
# Easy (44.98%)
# Total Accepted: 5K
# Total Submissions: 11.1K
# Testcase Example: '"a"\n"b"'
#
# 给定一个赎金信 (ransom)
# 字符串和一个杂志(magazine)字符串,判断第一个字符串ransom能不能由第二个字符串magazines里面的字符构成。如果可以构成,返回 true
# ;否则返回 false。
#
# (题目说明:为了不暴露赎金信字迹,要从杂志上搜索各个需要的字母,组成单词来表达意思。)
#
# 注意:
#
# 你可以假设两个字符串均只含有小写字母。
#
#
# canConstruct("a", "b") -> false
# canConstruct("aa", "ab") -> false
# canConstruct("aa", "aab") -> true
#
#
#
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
return all(ransomNote.count(c)<=magazine.count(c) for c in string.ascii_lowercase)

python粗暴式解法。

Leecode刷题之旅-C语言/python-383赎金信的更多相关文章

  1. Leecode刷题之旅-C语言/python-1.两数之和

    开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...

  2. Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符

    /* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...

  3. Leecode刷题之旅-C语言/python-28.实现strstr()

    /* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...

  4. Leecode刷题之旅-C语言/python-7.整数反转

    /* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...

  5. Leecode刷题之旅-C语言/python-434 字符串中的单词数

    /* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...

  6. Leecode刷题之旅-C语言/python-326 3的幂

    /* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...

  7. Leecode刷题之旅-C语言/python-263丑数

    /* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...

  8. Leecode刷题之旅-C语言/python-349两整数之和

    /* * @lc app=leetcode.cn id=371 lang=c * * [371] 两整数之和 * * https://leetcode-cn.com/problems/sum-of-t ...

  9. Leecode刷题之旅-C语言/python-349两个数组的交集

    /* * @lc app=leetcode.cn id=349 lang=c * * [349] 两个数组的交集 * * https://leetcode-cn.com/problems/inters ...

随机推荐

  1. CAShapeLayer的使用[2]

    CAShapeLayer的使用[2] CAShapeLayer支持的动画类型有如下这些. ------------------------------------------------------- ...

  2. JS实现手机摇一摇功能

    //运动事件监听 if (window.DeviceMotionEvent) { window.addEventListener('devicemotion',deviceMotionHandler, ...

  3. Kinaba 简单画图

    此片文章简单介绍如何在kinaba 上画图. 如果你,还没有搭建ELK 请参考:ELK日志分析平台搭建全过程 本文参考:http://www.cnblogs.com/hanyifeng/p/58607 ...

  4. [T-ARA][넘버나인][No.9]

    歌词来源: 넘버나인 (No.9):http://music.163.com/#/song?id=27808770 넘버나인(Club ver.):http://music.163.com/#/son ...

  5. window下使用SetUnhandledExceptionFilter捕获让程序的崩溃(转)

    简单使用SetUnhandledExceptionFilter()函数让程序优雅崩溃 虽然是大公司的产品,QQ它还是会在我们的折腾下崩溃的,但是它总是崩溃的很优雅,还要弹出自己的对话框来结束.并且发送 ...

  6. What Shape Layers Are-CAShapeLayer

    矢量图.gpu直接使用.占用内存小 What Shape Layers Are Shape layers are layers capable of defining shapes as vector ...

  7. GPU性能:光栅化、图层混合、离屏渲染

    So, shouldRasterize will not affect the green/red you see using Instruments. In order to have everyt ...

  8. Mac版Mysql Workbench不展示Schema

    Mac版的Mysql Workbench会不展示Schema,如下图 操作如下 cd ~/Library/Application\ Support/MySQL/Workbench/ rm wb_sta ...

  9. jQuery UI 实例 – 切换(Toggle)

    toggle()函数用于为每个匹配元素的click事件绑定轮流的处理函数. toggle()是一个特殊的事件函数,用于为匹配元素的click事件绑定多个事件处理函数.每次触发click事件时,togg ...

  10. ssrf绕过记录

    第一道题来自2018 上海市大学生网络安全大赛线上赛web01 if(isset($_POST['url']) && parse_url($_POST['url'])['host']= ...