Is a string an Anagram of another string?


How do you check if a word is an Anagram of another word? It’s reasonably simple, although I’m sure this can be simplified.

bool IsAnagram(string s1, string s2) {
    if (s1.Length != s2.Length) return false;
 
    var s1C = s1.ToCharArray();
    var s2C = s2.ToCharArray();
    Array.Sort(s1C);
    Array.Sort(s2C);
 
    return s1C.Equals(s2C);
}
Advertisements