Saturday 16 September 2017

Check whether two strings are anagram of each other

Write a function to check whether two given strings are anagram of each other or not. An anagram of a string is another string that contains same characters, only the order of characters can be different. For example, “abcd” and “dabc” are anagram of each other.

eg . listen=silent


class Program
    {
        static void Main(string[] args)
        {


            string strAnagram1 = "lisoen";
            string strAnagram2 = "silent";
            IEnumerable<byte> b1 = Encoding.ASCII.GetBytes(strAnagram1).OrderBy(x=>x);
            IEnumerable<byte> b2 = Encoding.ASCII.GetBytes(strAnagram2).OrderBy(x => x);
            bool isAnagram = true;
            if (strAnagram1.Length == strAnagram2.Length)
            {
                for (int i = 0;i< b1.Count(); i++)
                {
                    if (b1.ElementAt(i) != b2.ElementAt(i))
                    {
                        isAnagram = false;
                        break;
                    }
                }
            }

            if (isAnagram)
                Console.WriteLine("strings are anagram");
}
}

No comments:

Post a Comment

Recent Post

Parallel Task in .Net 4.0