Checking for Anagrams with LINQ
Anagrams are pairs of words or phrases that contain the same letters in a different order. This article describes a simple extension method that compares two strings using standard LINQ operators and determines if they are anagrams.
An anagram is a word or phrase that contains exactly the same letters as another word or phrase, except that the letters appear in a different order. For example, the word "star" has the anagrams "rats" and "tars". Longer anagrams may include several words, spaces and punctuation. However, only the letters need match. For example, "Quid est veritas?" is an anagram of "Est vir qui adest" despite the differing number of spaces and the question mark.
In this article we will create a method that determines if two provided strings are anagrams. The example code will define an extension method to the string class.
Creating the Extension Method
The first step is to create a class to contain the extension method. Create a new project and add the following definition. This type will hold the extension method so is declared as a static class:
public static class AnagramTester
The method, "IsAnagramOf", accepts the two strings to compare as arguments and returns a Boolean value that is true if the strings are anagrams. The first parameter is declared with the this prefix to indicate that the static method is an extension method for strings.
public static bool IsAnagramOf(this string stringA, string stringB)
We can now add the code that performs the comparison. The first step is to convert the strings into objects that can be compared correctly. Firstly the strings are converted to lower case, as anagrams should not be case-sensitive. Next the individual characters are extracted from into arrays using the ToArray method.
One we have the character arrays we can use LINQ and the Where standard query operator to filter those arrays. For each string, any characters that are not part of the standard English alphabet are removed by the first two lines of code. This eliminates spaces and punctuation. The final line of the method sorts the resultant arrays and compares them using SequenceEqual. If the sorted arrays match, the original strings must be anagrams and the method returns true.
var charsA = stringA.ToLower().ToArray().Where(c => c >= 'a' && c <= 'z');
var charsB = stringB.ToLower().ToArray().Where(c => c >= 'a' && c <= 'z');
return charsA.OrderBy(c => c).SequenceEqual(charsB.OrderBy(c => c));
We can perform a simple test by executing the method as follows:
bool isAnagram = "Quid est veritas?".IsAnagramOf("Est vir qui adest"); // true
9 February 2011