【问题标题】:Implementation of an anagram function in C#在 C# 中实现 anagram 函数
【发布时间】:2011-10-27 17:20:35
【问题描述】:

可能重复:
What is an easy way to tell if a list of words are anagrams of each other?

在 C# 中编写一个函数的最佳方法(性能范围)是什么,该函数接受两个字符串,当字符串是彼此的字谜时返回 true,否则返回 false。字谜的例子是:

abet beat beta bate
abides biased

anagrams link

在实现这个的时候,每个字符串中是否有可能有空格?

任何想法将不胜感激!

【问题讨论】:

标签: c#


【解决方案1】:

使用 LINQ 的简单(幼稚?)方式:

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

【讨论】:

【解决方案2】:

一个简单的解决方案是按字母顺序对字符进行排序并将它们相互比较。

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

然后,使用它:

    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));
    }   

这种情况下的输出是

True

False

【讨论】:

    【解决方案3】:

    如何不这样做:从每个字符串中删除所有空格。使用Algorithm to generate anagrams 中的一种算法生成第一个字符串的所有可能排列。最后,在排列列表中搜索匹配项;如果有一个,那么这两个是字谜,否则,不是。

    【讨论】:

      【解决方案4】:

      我有一个字符串列表(不仅仅是两个字符串)的解决方案。 如果你对它或任何其他感兴趣,你可以使用它。

      给定一个字符串数组,删除之前字符串的变位词的每个字符串,然后按存储顺序返回剩余的数组。

      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;
          }
      

      【讨论】:

        【解决方案5】:

        并计算所有场景 (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;
                } 
        

        【讨论】:

          猜你喜欢
          • 2015-11-15
          • 1970-01-01
          • 2013-10-18
          • 1970-01-01
          • 2023-01-28
          • 2018-11-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多