1public boolean canConstruct(String ransomNote, String magazine) {
2 // Create a map to store character frequencies from the magazine
3 Map<Character, Integer> magazineFreq = new HashMap<>();
4
5 // Fill the frequency map with characters from the magazine
6 for (char ch : magazine.toCharArray()) {
7 magazineFreq.put(ch, magazineFreq.getOrDefault(ch, 0) + 1);
8 }
9
10 // Check against the frequency map with each character from ransomNote
11 for (char ch : ransomNote.toCharArray()) {
12 // Check if the character is missing or not enough in the magazine
13 if (!magazineFreq.containsKey(ch) || magazineFreq.get(ch) == 0) {
14 return false;
15 }
16 // Decrease the frequency count for the current character
17 magazineFreq.put(ch, magazineFreq.get(ch) - 1);
18 }
19
20 return true;
21}