Hash Tables: Ransom Note

  • + 0 comments
    // Java
    public static void checkMagazine(List<String> magazine, List<String> note) {
        Map<String, Integer> magazineMap = new HashMap<>();
        Map<String, Integer> noteMap = new HashMap<>();
        for(String m : magazine){
            magazineMap.put(m, magazineMap.getOrDefault(m, 0)+1);
        }
        for(String n : note){
            noteMap.put(n, noteMap.getOrDefault(n, 0)+1);
        }
        for (Map.Entry<String, Integer> entry : noteMap.entrySet()) {
                if (magazineMap.getOrDefault(entry.getKey(), 0) < entry.getValue()) {
                    System.out.println("No");
                    return;
                }
            }
            System.out.println("Yes");
    
        }
    
    }