Hash Tables: Ransom Note

Sort by

recency

|

1932 Discussions

|

  • + 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");
    }
    
  • + 0 comments

    JAVA 18 public static void checkMagazine(List magazine, List note) {

    boolean isFound = true;
    for(String word : note){
        if(!magazine.contains(word)){
            isFound = false;
        }else{
            magazine.remove(word);
        }
    }
    if(isFound){
        System.out.println("Yes");
    }else{
        System.out.println("No");
    }
    
    }
    

    }

  • + 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)