Hash Tables: Ransom Note

  • + 0 comments

    Java 15

        public static void checkMagazine(List<String> magazine, List<String> note) {
        // Write your code here
            Map<String,Long> magMap = magazine.stream().collect(Collectors.groupingBy(Function.identity(),Collectors.counting()));
            
            String isPossible = "Yes";
            
            for(String str: note){
                
                if(magMap.get(str) == null || magMap.get(str) == 0){
                    isPossible = "No";
                    break;
                }else{
                    magMap.put(str, magMap.get(str)-1);
                }
            }
            System.out.println(isPossible);
    
        }