开发者

Implementation of an anagram function in C#

Possible Duplicate:

What is an easy way to tell if a list of words are anagrams of each other?

What is the best way (performance wide) to write a function in C# that takes two strings and returns true when the strings are anagrams of each other and otherwise returns false. Example of anagrams are:

abet beat beta bate
abides biased

anagrams link

In implementing this, is it possible that there is space in each string?

Any idea would be ver开发者_开发知识库y much appreciated!


A simple (naïve?) way, using LINQ:

"abides".OrderBy(c=>c).SequenceEqual("biased".OrderBy(c=>c))


An easy solution would be to sort the characters alphabetically and compare them to one another.

public static class AnagramExtensions
{
    public static bool IsAnagramOf(this string word1, string word2)
    {
        return word1.OrderBy(x => x).SequenceEqual(word2.OrderBy(x => x));
    }
}

Then, to use it:

    static void Main()
    {
        string word1 = "cat";
        string word2 = "tac";

        Console.WriteLine(word1.IsAnagramOf(word2));

        string word3 = "cat";
        string word4 = "dog";

        Console.WriteLine(word3.IsAnagramOf(word4));
    }   

The output in this case would be

True

False


How not to do this: Remove all whitespace from each of the strings. Use one of the algorithms at Algorithm to generate anagrams to generate all possible permutations of the first string. Finally, search the list of permuations for a match; if there is one, then the two are anagrams, otherwise, not.


I have a solution for a List of Strings (Not just two Strings). If you are interested in it or any other one, you can use it.

Given an array of strings, remove each string that is an anagram of an earlier string, then return the remaining array in stored order.

private static List<string> GencoAnagrams(List<string> textList)
    {
        var listCount = textList.Count;
        if (listCount == 1) return textList;

        for (var j = 1; j < listCount; j++)
        {
            for (var i = 0; i < j; i++)
            {
                if (string.Concat(textList[j].OrderBy(x => x)).Equals(string.Concat(textList[i].OrderBy(y => y))))
                {
                    textList.RemoveAt(j);
                    --listCount;
                    --j;
                    if (listCount == 1) break;
                }
            }
            if (listCount == 1) break;
        }
        return textList;
    }


and count all sceniro (n*n+1)/2

public static int sherlockAndAnagrams(string s)
        {
            int count = 0;
            string[] stringList = new string[(s.Length * (s.Length + 1)) / 2];
            int index = 0;
            Dictionary<string, int> hash = new Dictionary<string, int>();
            for (int i = 0; i < s.Length; i++)
            {
                for (int j = i; j < s.Length; j++)
                {
                    stringList[index] = s.Substring(i, j - i + 1);
                    index++;
                }
            }
            foreach (var str in stringList)
            {
                var keyString = string.Concat(str.OrderBy(x => x));
                if (hash.ContainsKey(keyString))
                {
                    hash[keyString]++;
                }
                else
                {
                    hash[keyString] = 1;
                }
            }
            foreach (var key in hash.Keys)
            {
                if (hash[key] <= 1)
                {
                    continue;
                }
                else
                {
                    count = count + ((hash[key] * (hash[key] - 1) )/ 2);
                }
            }
            return count;
        } 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜