/*
* @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. Oracle - ORA-28547: Connection to server failed,probable Oracle Net admin error (Navicat)

    一.异常 用Navicat连接Oracle数据库时抛出的异常 二.方案 使用 Oracle 安装目录 \Oracle\product\11.2.0\dbhome_1\BIN 下的 oci.dll 替换 ...

  2. iOS8模糊效果UIVisualEffectView的使用

    iOS8模糊效果UIVisualEffectView的使用 效果: 源码: // // ViewController.m // EffectView // // Created by XianMing ...

  3. 安装SCOM Reporting Server

    在SQL群集计算机上可以安装SCOM Reporting Server. 1.运行SQL Server安装程序,选择 全新安装SQL Server,不能向已有的群集实例中添加Reporting Ser ...

  4. Python实例---抽屉后台框架分析

    1.1. 抽屉框架分析 --登陆注册分析 1.2. 前台获取form表单补充知识: <!DOCTYPE html> <html lang="en"> < ...

  5. Java实例---简单的超市管理系统

    代码分析 Customer.java package test; public class Customer { private String name; private int customerTy ...

  6. December 21st 2016 Week 52nd Wednesday

    Keep conscience clear, then never fear. 问心无愧,永不畏惧. I find it is very difficult for me to keep consci ...

  7. 读 CSI讲义 费马小定理

    费马小定理 最近在上计算机安全学选修课.. 读老师博客..现在当是写阅读笔记吧. 这里贴出老师的简书建议先看看链接先..毕竟我这些东西只是搞笑一下的.. 遵循一下这个原则… 观察 找规律 求证 首先是 ...

  8. 让IE6、7、8兼容@media属性

    通常做页面适配的时候,经常会用到@media属性,对不同屏幕范围内的元素设置不同的样式.但是@media属性不兼容IE8及IE8以下的浏览器 解决方法: 直接在页面中引入respond.src.js即 ...

  9. canvas抛物线运动demo

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  10. hdu-3397 Sequence operation 线段树多种标记

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3397 题目大意: 0 a b表示a-b区间置为0 1 a b表示a-b区间置为1 2 a b表示a- ...