public Anagram.Models.AnagramDataModel GetFormattedAnagrams(String phrase)
        {
            String [] resultsArray = GetAnagrams(phrase).ToArray();

            var phraseLetterValues = new LetterValues(phrase);

            if (resultsArray.Length >= 2)
            {
                var watch = Stopwatch.StartNew();


                Parallel.ForEach(Combinatorics.Combinations <String>(resultsArray, 2, false), (combination) =>
                {
                    if (CheckIfStringsHaveTheSameFrequency(combination, phrase))
                    {
                        AnagramsFormattedResults.Add(ComposeAnagramsStrings(combination));
                    }
                });

                watch.Stop();

                return(new Models.AnagramDataModel(AnagramsFormattedResults, watch.ElapsedMilliseconds));
            }
            return(new Models.AnagramDataModel(new List <string>(), 0));
        }
Example #2
0
        public LetterValues GetWordFromPhrase(LetterValues newWordValues)
        {
            LetterValues result = new LetterValues(String.Empty);

            Int32[] original     = letterValues;
            Int32[] checkedValue = newWordValues.letterValues;

            for (int i = 0; i < result.letterValues.Length; i++)
            {
                result.letterValues[i] = original[i] - checkedValue[i];

                if (result.letterValues[i] < 0)
                {
                    return(null);
                }
            }
            return(result);
        }
        private List <String> GetAnagrams(String phrase)
        {
            if (string.IsNullOrWhiteSpace(phrase))
            {
                throw new ArgumentNullException();
            }

            WordDictionary = _repository.ReadData();

            var phraseValues = new LetterValues(phrase);

            foreach (var word in WordDictionary)
            {
                var newWord = new LetterValues(word);

                if (phraseValues.GetWordFromPhrase(newWord) != null)
                {
                    Anagrams.Add(word);
                }
            }
            return(Anagrams);
        }