温州快速网站建设排名,路飞和女帝做h的网站,手机erp系统免费版,佛山企业如何建网站给你两个字符串#xff1a;ransomNote 和 magazine #xff0c;判断 ransomNote 能不能由 magazine 里面的字符构成。
如果可以#xff0c;返回 true #xff1b;否则返回 false 。
magazine 中的每个字符只能在 ransomNote 中使用一次。
class Solution {public boolea…给你两个字符串ransomNote 和 magazine 判断 ransomNote 能不能由 magazine 里面的字符构成。
如果可以返回 true 否则返回 false 。
magazine 中的每个字符只能在 ransomNote 中使用一次。
class Solution {public boolean canConstruct(String ransomNote, String magazine) {HashMapCharacter,Integer hm new HashMap();for(int i0;imagazine.length();i){char a magazine.charAt(i);if(hm.containsKey(a))hm.put(a, hm.get(a)1);elsehm.put(a,0);}for(int i0; iransomNote.length(); i){char b ransomNote.charAt(i);// hm不包含b, 返回falseif(!hm.containsKey(b))return false;// hm包含b, 若可使用次数不足, 返回falseif(hm.get(b)0)return false;// hm包含b且可使用更新可使用次数hm.put(b, hm.get(b)-1);}return true;}
}