Hash Tables: Ransom Note

Sort by

recency

|

1931 Discussions

|

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