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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s