You are viewing a single comment's thread. Return to all comments →
This is perfect. I tried same logic in C# and it's superfast.
static bool checkwords(string[] mags, string[] rans) { Dictionary<string, int> table = new Dictionary<string, int>(); foreach (string s in mags) { if (!table.ContainsKey(s)) { table[s] = 1; } else { table[s] += 1; } } foreach (string s in rans) { if (!table.ContainsKey(s)) { table[s] = 0; } else if (table[s] == 0) { return false; } else { table[s] -= 1; } } return true; } static void Main(string[] args) { 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(' '); if (n > m) { Console.WriteLine("No"); } else if (checkwords(magazine, ransom)) { Console.WriteLine("Yes"); } else { Console.WriteLine("No"); } }
Seems like cookies are disabled on this browser, please enable them to open this website
Hash Tables: Ransom Note
You are viewing a single comment's thread. Return to all comments →
This is perfect. I tried same logic in C# and it's superfast.