namespace _383._Ransom_Note; public class Solution { public bool CanConstruct(string ransomNote, string magazine) { if (ransomNote.Length > magazine.Length) return false; Dictionary note = CountChars(ransomNote); Dictionary mag = CountChars(magazine); foreach (var kp in note) { if (mag.ContainsKey(kp.Key)) { if (mag[kp.Key] < kp.Value) return false; } else return false; } return true; } private Dictionary CountChars(string str) { Dictionary dict = new Dictionary(); foreach (char ch in str) { if (dict.ContainsKey(ch)) dict[ch]++; else dict.Add(ch, 1); } return dict; } }