赎金信 (Ransom Note)

 

思路:

// @Title: 赎金信 (Ransom Note)
// @Author: qisiii
// @Date: 2024-09-12 01:51:33
// @Runtime: 10 ms
// @Memory: 44.2 MB
// @comment: 
// @flag: 
class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        HashMap<Character,Integer> map=new HashMap<>();
        for(Character c:magazine.toCharArray()){
            map.put(c,map.getOrDefault(c,0)+1);
        }
        for(Character c:ransomNote.toCharArray()){
            if(!map.containsKey(c)){
                return false;
            }
            int value=map.get(c);
            if(value<=0){
                return false;
            }
            map.put(c,map.get(c)-1);
        }
        return true;
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18