Check whether two Strings are anagram of each other
public class Solution
{
bool AreAnagrams(string str1, string str2)
{
if (str1.Length != str2.Length)
return false;
var dict = new Dictionary<char, int>();
foreach (char c in str1)
{
if (dict.ContainsKey(c))
dict[c]++;
else
dict[c] = 1;
}
foreach (char c in str2)
{
if (!dict.ContainsKey(c))
return false;
if (--dict[c] == 0)
dict.Remove(c);
}
return dict.Count == 0;
}
}Last updated