Finding anagrams for a given word

后端 未结 12 1068
温柔的废话
温柔的废话 2020-12-04 07:43

Two words are anagrams if one of them has exactly same characters as that of the another word.

Example : Anagram & Nagaram are anagrams

12条回答
  •  囚心锁ツ
    2020-12-04 08:12

    static void Main(string[] args)
    {
    
        string str1 = "Tom Marvolo Riddle";
        string str2 = "I am Lord Voldemort";
    
        str2=  str2.Replace(" ", string.Empty);
        str1 = str1.Replace(" ", string.Empty);
        if (str1.Length != str2.Length)
            Console.WriteLine("Strings are not anagram");
        else
        {
            str1 = str1.ToUpper();
            str2 = str2.ToUpper();
            int countStr1 = 0;
            int countStr2 = 0;
            for (int i = 0; i < str1.Length; i++)
            {
                countStr1 += str1[i];
                countStr2 += str2[i];
    
            }
            if(countStr2!=countStr1)
                Console.WriteLine("Strings are not anagram");
            else Console.WriteLine("Strings are  anagram");
    
        }
        Console.Read();
    }
    

提交回复
热议问题