Hash Tables: Ransom Note

  • + 0 comments

    Java:

    public static void checkMagazine(List<String> magazine, List<String> note) {
            // Write your code here
            Map<String, Integer> map = new HashMap<>();
            Map<String, Integer> noteMap = new HashMap<>();
            for(String str: magazine) {
                if(map.containsKey(str)) map.put(str, map.get(str)+1);
                else map.put(str, 1);
            }
            for(String str: note) {
                if(noteMap.containsKey(str)) noteMap.put(str, noteMap.get(str)+1);
                else noteMap.put(str, 1);
            }
            String ans ="Yes";
            for (Map.Entry<String, Integer> set:noteMap.entrySet()) {
                if(!map.containsKey(set.getKey()) ||
                        (map.containsKey(set.getKey()) && map.get(set.getKey())<noteMap.get(set.getKey()))) {
                    ans = "No";
                }
            }
            System.out.println(ans);
    
    }