You are viewing a single comment's thread. Return to all comments →
After struggling quite a bit, I found this answers but it didn't quite work, so I made a few changes. Here's the modified method:
static void checkMagazine(string[] mags, string[] rans) {
if (rans.Length > mags.Length) { Console.WriteLine("No"); return; } Dictionary<string, int> table = new Dictionary<string, int>(); bool isValid = true; for (int i = 0; i < mags.Length; i++) { if (!table.ContainsKey(mags[i])) { table.Add(mags[i], 1); } else { table[mags[i]] += 1; } } for (int i = 0; i < rans.Length; i++) { if (!table.ContainsKey(rans[i])) { isValid = false; break; } else if (table[rans[i]] == 0) { isValid = false; break; } else { table[rans[i]] -= 1; } } Console.WriteLine(isValid ? "Yes" : "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 →
After struggling quite a bit, I found this answers but it didn't quite work, so I made a few changes. Here's the modified method:
static void checkMagazine(string[] mags, string[] rans) {