leetcode 383 赎金信

简介: leetcode 383 赎金信

赎金信


a5a5838633f44f85905daa3b729d6a17.png

#include <iostream>
#include<string>
#include<vector>
#include<unordered_map>
#include <algorithm>
using namespace std;
class Solution {
public:
    bool canConstruct(string ransomNote, string magazine) {
        unordered_map<char, int> num_map;
        for (auto i : magazine)
        {
            num_map[i]++;
        }
        for (auto i : ransomNote)
        {
            num_map[i]--;
            if ( num_map[i]==-1)
                return false;
        }
        return true;
    }
};
int main() {
    Solution a;
    string ransomNote = "a";
    string magazine = "b";
    cout<<a.canConstruct( ransomNote,   magazine);
    return 0;
}

二刷

class Solution {
public:
    bool canConstruct(string ransomNote, string magazine) {
        unordered_map<char,int> my_map;
        for(int i=0 ; i<magazine.size();i++)
            my_map[magazine[i]]++;
        for(int i=0 ; i<ransomNote.size();i++)
            my_map[ransomNote[i]]--;
        for(auto it:my_map)
            if(it.second < 0) return false;
        return true;
    }
};
相关文章
|
2月前
|
Java C++ Python
leetcode-383:赎金信
leetcode-383:赎金信
31 1
|
2月前
|
Java
383. 赎金信 --力扣 --JAVA
给你两个字符串:ransomNote 和 magazine ,判断 ransomNote 能c里面的字符构成。 如果可以,返回 true ;否则返回 false 。 magazine 中的每个字符只能在 ransomNote 中使用一次。
29 1
|
9月前
|
索引
【Leetcode -383.赎金信 -387.字符串中的第一个唯一字符】
【Leetcode -383.赎金信 -387.字符串中的第一个唯一字符】
29 0
|
7天前
|
存储
力扣经典150题第三十九题:赎金信
力扣经典150题第三十九题:赎金信
4 0
|
2月前
【力扣】383.赎金信
【力扣】383.赎金信
|
2月前
|
存储 算法 Java
[Java·算法·简单] LeetCode 383. 赎金信 详细解读
[Java·算法·简单] LeetCode 383. 赎金信 详细解读
32 0
|
11月前
LeetCode150道面试经典题--赎金信(简单)
给你两个字符串:ransomNote 和 magazine ,判断 ransomNote 能不能由 magazine 里面的字符构成。
50 0
|
2月前
|
算法
六六力扣刷题哈希表之赎金信
六六力扣刷题哈希表之赎金信
23 0
|
8月前
|
算法
代码随想录算法训练营第七天 | LeetCode 454.四数相加II、383. 赎金信、15. 三数之和、18. 四数之和
代码随想录算法训练营第七天 | LeetCode 454.四数相加II、383. 赎金信、15. 三数之和、18. 四数之和
31 0