public bool IsAnagram(string wordA, string wordB)
{
if (wordA.Length != wordB.Length) return false;
var dict = new Dictionary<char, int>();
foreach (var c in wordA)
{
dict[c] = dict.GetValueOrDefault(c, 0) + 1;
}
foreach (var c in wordB)
{
if (!dict.ContainsKey(c)) return false;
dict[c]--;
if (dict[c] < 0) return false;
}
return true;
}
Aucun commentaire:
Enregistrer un commentaire