Hash Tables: Ransom Note

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