Hash Tables: Ransom Note

Sort by

recency

|

1929 Discussions

|

  • + 0 comments

    It looks there is an issue with the fucntion definition. It is a void function. how can we return the result?

  • + 0 comments
    def checkMagazine(magazine, note):
        # Write your code here
        
        word_count = {}
        flag = 'Yes'
        
        for i in magazine:
            if i in word_count:
                word_count[i] += 1
            else:
                word_count[i] = 1
        
        for i in note:
            if i in word_count and word_count[i] > 0:
                word_count[i] -= 1
            else:
                flag = 'No'
            
        print(flag)
    
  • + 0 comments

    Go solution pretty simple

    	dic := make(map[string]int)
    	for _, n := range magazine {
    		dic[n]++
    	}
    	for _, n := range note {
    		if val, ok := dic[n]; !ok || val == 0 {
    			fmt.Println("No")
    			return
    		} else {
    			dic[n]--
    		}
    	}
    	fmt.Println("Yes")
    }
    
  • + 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);
    
        }
    
  • + 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");
    
        }
    
    }