private static bool isAnagram(string textA, string textB) { Dictionary<char, int> texTA = new Dictionary<char, int>(); //Dictionary<char, int> texTB = new Dictionary<char, int>(); if (textA.Length == textB.Length) { for (int i = 0; i < textA.Length; i++) { if (texTA.ContainsKey(textA[i])) { texTA[textA[i]]++; } else { texTA.Add(textA[i], 1); } } for (int i = 0; i < textB.Length; i++) { if (texTA.ContainsKey(textB[i])) { } else { return false; } } } else { return false; } return true; }
public static bool CheckAnagram(string wordA, string wordB) { Dictionary<char, int> A = new Dictionary<char, int>(); foreach (var element in wordA) { if (A.ContainsKey(element)) { A[element] += 1; } else { A.Add(element, 1); } } Dictionary<char, int> B = new Dictionary<char, int>(); foreach (var element in wordB) { if (B.ContainsKey(element)) { B[element] += 1; } else { B.Add(element, 1); } } bool equalAB = A.OrderBy(r => r.Key).SequenceEqual(B.OrderBy(r => r.Key)); return equalAB; }