You are viewing a single comment's thread. Return to all comments →
Hi, I'm new here and have been reading all the posts. Would love to get feedback on my C# code. This also handles the issue of duplicates.
string[] tokens_m = Console.ReadLine().Split(' '); int m = Convert.ToInt32(tokens_m[0]); int n = Convert.ToInt32(tokens_m[1]); string[] magazine = Console.ReadLine().Split(' '); string[] ransom = Console.ReadLine().Split(' '); string output = "Yes"; Dictionary<string, int> dictMagWords = new Dictionary<string, int>(); foreach (string word in magazine) { if (dictMagWords.ContainsKey(word)) dictMagWords[word] += 1; else dictMagWords.Add(word, 1); } for(int count = 0; count < ransom.Length; count++) { if(dictMagWords.ContainsKey(ransom[count])) { dictMagWords[ransom[count]] -= 1; if (dictMagWords[ransom[count]] == 0) dictMagWords.Remove(ransom[count]); } else { output = "No"; break; } } Console.WriteLine(output);
Hash Tables: Ransom Note
You are viewing a single comment's thread. Return to all comments →
Hi, I'm new here and have been reading all the posts. Would love to get feedback on my C# code. This also handles the issue of duplicates.