public static IRepresentOrderdString FromString(string word)
        {
            IRepresentOrderdString x = new IRepresentOrderdString();

            for (int i = 0; i < word.Length; i++)
            {
                var index = word[i] - 'A';
                if (index < 0) //store all characters that come beofe 'A' in the 7th decimal place in the socond ulong character
                {
                    x.two += Math.Pow(10, 7);
                }
                else if (index <= 18) //store the count of characters A-S in the fist ulong
                {
                    x.one += Math.Pow(10, index);
                }
                else if (index <= 37) //store the count of characters T-Z a-f in the second ulong
                {
                    index -= 19;
                    x.two += Math.Pow(10, index);
                }
                else if (index <= 57) //store the count of characters g-z in the third ulong
                {
                    index   -= 38;
                    x.three += Math.Pow(10, index);
                }
                else //store all other/special characters count in the 9th decimal place in the second ulong
                {
                    x.two += Math.Pow(10, 8);
                }
            }

            return(x);
        }
        public string[] FindAllAnagrams(string[] words)
        {
            var anagrams = words
                           .AsParallel()
                           .GroupBy(w => IRepresentOrderdString.FromString(w))
                           .Where(g => g.Count() > 1)
                           .Select(x => string.Join(" ", x))
                           .ToArray();

            return(anagrams.ToArray());
        }
        public string[] FindAllAnagrams(string[] words)
        {
            var dic = new ConcurrentDictionary <IRepresentOrderdString, string>();

            var parallelLoopResult = Parallel.ForEach(words, word =>
            {
                var key = IRepresentOrderdString.FromString(word);
                dic.AddOrUpdate(key, word, (_, currentValue) => currentValue + " " + word);
            });

            return(dic.Values.Where(x => x.Contains(" ")).ToArray());
        }
示例#4
0
    public void Test(string word, string expected)
    {
        var representOrderdString = IRepresentOrderdString.FromString(word);

        Assert.AreEqual(expected, representOrderdString.ToString());
    }