C++ 383. 赎金信 (a b字符串计数比较)

给你两个字符串:ransomNote 和 magazine ,判断 ransomNote 能不能由 magazine 里面的字符构成。

如果可以,返回 true ;否则返回 false 。

magazine 中的每个字符只能在 ransomNote 中使用一次。

示例 1:

输入:ransomNote = “a”, magazine = “b”
输出:false
示例 2:

输入:ransomNote = “aa”, magazine = “ab”
输出:false
示例 3:

输入:ransomNote = “aa”, magazine = “aab”
输出:true

提示:

1 <= ransomNote.length, magazine.length <= 105
ransomNote 和 magazine 由小写英文字母组成

class Solution {
   
public:
    bool canConstruct(string ransomNote, string magazine) {
   
        int a[26] = {
   0}, b[26] = {
   0};
        for(auto x : ransomNote) a[x - 'a'] ++;
        for(auto x : magazine) b[x - 'a'] ++;
        bool res = true;
        for(int i = 0; i < 26; i ++) {
   
            if(a[i] > b[i])
                res = false;
        }
        return res;
    }
};

相关推荐

  1. C++ 383. 赎金 (a b字符串计数比较)

    2023-12-29 03:16:02       35 阅读
  2. 383. 赎金

    2023-12-29 03:16:02       43 阅读
  3. LeetCode383赎金

    2023-12-29 03:16:02       19 阅读
  4. 383.赎金

    2023-12-29 03:16:02       18 阅读
  5. 383. 赎金

    2023-12-29 03:16:02       22 阅读
  6. 【LeetCode 383赎金

    2023-12-29 03:16:02       11 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-29 03:16:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-29 03:16:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-29 03:16:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-29 03:16:02       20 阅读

热门阅读

  1. Ubuntu系统更换清华大学镜像源

    2023-12-29 03:16:02       42 阅读
  2. leetcode-2.两数相加

    2023-12-29 03:16:02       38 阅读
  3. 3.01【python正则表达式以及re模块】

    2023-12-29 03:16:02       40 阅读
  4. Mysql5.7版本实现窗口函数

    2023-12-29 03:16:02       32 阅读
  5. 基于SpringBoot的考编论坛网站

    2023-12-29 03:16:02       43 阅读
  6. python mysql登录注册

    2023-12-29 03:16:02       38 阅读
  7. C++如何获取随机浮点数

    2023-12-29 03:16:02       37 阅读
  8. LeetCode //C - 1732. Find the Highest Altitude

    2023-12-29 03:16:02       34 阅读