zoukankan      html  css  js  c++  java
  • 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


    第二个字符串包含第一个字符串的所有字母,就返回true,不一定要连续包含
    只要第一个字符串中某个字母的数量 大于 第二个字符串的这个字母的数量 则返回false

    C++(19ms):

     1 class Solution {
     2 public:
     3     bool canConstruct(string ransomNote, string magazine) {
     4         vector<int> magazLetters(128, 0);
     5         for(char m : magazine)
     6             magazLetters[m]++;
     7         for(char r : ransomNote)
     8             if(--magazLetters[r]<0) return false;
     9         return true;
    10     }
    11 };
  • 相关阅读:
    centos 配置静态ip
    mysql常用命令
    mac 安装好mysql后密码重置
    安装Intellij Idea14/15
    freemarker 学习一 入门小例子
    获取类路径
    mysql中的int smallint 取值范围
    MySQL按照汉字的拼音排序
    Log4j 基本配置
    追加写入
  • 原文地址:https://www.cnblogs.com/mengchunchen/p/6618138.html
Copyright © 2011-2022 走看看