classSolution{publicbooleancanConstruct(String ransomNote,String magazine){Map<String,Integer> map =newHashMap<>();for(int i =0; i < magazine.length(); i++){String word =String.valueOf(magazine.charAt(i));map.put(word, map.getOrDefault(word,0)+1);}for(int i =0; i < ransomNote.length(); i++){String word =String.valueOf(ransomNote.charAt(i));if(!map.containsKey(word)){returnfalse;}if(map.get(word)<1){returnfalse;}map.put(word, map.getOrDefault(word,0)-1);}returntrue;}}
// 代码随想录提供的题解classSolution{publicbooleancanConstruct(String ransomNote,String magazine){if(ransomNote.length()> magazine.length()){returnfalse;}int[] judge =newint[26];for(char word : magazine.toCharArray()){judge[word -'a']+=1;}for(char c : ransomNote.toCharArray()){judge[c -'a']-=1;}for(int i : judge){if(i <=0){returnfalse;}}returntrue;}}