Hash Tables: Ransom Note

Sort by

recency

|

1935 Discussions

|

  • + 0 comments
    def checkMagazine(magazine, note):
        result = 0
        word_dict = {}
        
        for i in range(len(magazine)):
          word_dict[magazine[i]] = word_dict.get(magazine[i], 0) + 1
    
        for word in note:
          word_dict[word] = word_dict.get(word, 0) - 1
          if word_dict[word] < 0:
              return print("No")
        
        print("Yes")
    
  • + 0 comments
    "def checkMagazine(magazine, note):
        count_mag = Counter(magazine)
        count_note = Counter(note)
        
        for word in count_note.keys(): 
            if word not in count_mag.keys() or count_note[word] > count_mag[word]:
                print(""No"")
                return 
        print(""Yes"")
    "
    
  • + 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);
    
    }
    
  • + 0 comments

    ts

    function checkMagazine(magazine: string[], note: string[]): void {
        if(magazine.length < note.length){
            console.log("No")
            return
        }
            
        const magazineMap = new Map<string, number>()
        magazine.forEach( (magazineWord) => {
            const value = magazineMap.get(magazineWord) ? magazineMap.get(magazineWord) + 1 : 1
            magazineMap.set(magazineWord, value)
        })
        
        for(const word of note){
            if(!magazineMap.has(word)){
                console.log("No")
                return
            }
            const currentValue = magazineMap.get(word)
            if(currentValue - 1 === 0){
                magazineMap.delete(word)
            }else {
                magazineMap.set(word, currentValue -1)
            }
        }
        console.log("Yes")
    }
    
  • + 0 comments

    Solution for c#

    public static void checkMagazine(List<string> magazine, List<string> note)
    {
    	Dictionary<string, int> magazineDict = magazine.GroupBy(x => x)
    			.ToDictionary(x => x.Key, g => g.Count());
    
    	foreach(string sNote in note){
    		if (magazineDict.ContainsKey(sNote) && magazineDict[sNote]>0) {
    			 magazineDict[sNote]--;
    		} else { 
    			Console.WriteLine("No"); 
    			return;
    		}
    	}
    
    	Console.WriteLine("Yes");
    }