5

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 very much appreciated!

Community
  • 1
  • 1
Pingpong
  • 7,681
  • 21
  • 83
  • 209
  • Homework? If so please tag this as such. – Yuck Aug 16 '11 at 02:10
  • does this help: http://stackoverflow.com/questions/522112/what-is-an-easy-way-to-tell-if-a-list-of-words-are-anagrams-of-each-other – shelleybutterfly Aug 16 '11 at 02:11
  • Possible duplicate of [What is an easy way to tell if a list of words are anagrams of each other?](https://stackoverflow.com/questions/522112/what-is-an-easy-way-to-tell-if-a-list-of-words-are-anagrams-of-each-other) – Cœur Jul 14 '18 at 06:58

5 Answers5

8

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

"abides".OrderBy(c=>c).SequenceEqual("biased".OrderBy(c=>c))
porges
  • 30,133
  • 4
  • 83
  • 114
  • +1 very succinct, says in one line what some of the solutions at http://stackoverflow.com/questions/522112/what-is-an-easy-way-to-tell-if-a-list-of-words-are-anagrams-of-each-other take half a page to say. :) – shelleybutterfly Aug 16 '11 at 02:15
7

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

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
0

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;
    }
Muhammed_G
  • 375
  • 3
  • 7
0

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;
        } 
tayfun Kılıç
  • 2,042
  • 1
  • 14
  • 11
0

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.

Community
  • 1
  • 1
shelleybutterfly
  • 3,216
  • 15
  • 32