private static void WriteResults(AnagramFinderResult result) { Console.WriteLine(); Console.WriteLine(); Console.WriteLine($"Results: {Environment.NewLine}"); foreach (var list in result.Anagrams) { WriteList(list); } Console.WriteLine(); Console.Write("The longest word with anagram (first in their list) is "); Console.ForegroundColor = QuiteColor; Console.Write($"{result.GetLongestWordWithAnagram()}"); Console.ForegroundColor = NormalColor; Console.WriteLine(); Console.WriteLine(); Console.Write("The longest anagram list is: "); Console.ForegroundColor = QuiteColor; WriteList(result.GetLongestAnagramList()); Console.ForegroundColor = NormalColor; }
public AnagramFinderResult FindAnagrams(List <string> words) { var result = new AnagramFinderResult(); var anagramsNumber = new HashSet <ushort>(); var anagramChecker = new AnagramChecker(); var longestWordWithAnagramLength = 0; var longestAnagramListLength = 0; for (var i = 0; i < words.Count - 1; i++) { if (anagramsNumber.Contains((ushort)i)) { continue; } var currentWord = words[i]; List <string> currentWordAnagrams = null; for (var j = i + 1; j < words.Count; j++) { var isAnagram = anagramChecker.Check(currentWord, words[j]); if (isAnagram) { anagramsNumber.Add((ushort)j); if (currentWordAnagrams == null) { currentWordAnagrams = new List <string>() { currentWord, words[j] }; } else { currentWordAnagrams.Add(words[j]); } } } if (currentWordAnagrams != null) { if (longestAnagramListLength < currentWordAnagrams.Count) { longestAnagramListLength = currentWordAnagrams.Count; result.LongestListIndex = (ushort)result.Anagrams.Count; } if (longestWordWithAnagramLength < currentWord.Length) { longestWordWithAnagramLength = currentWord.Length; result.LongestWordIndex = (ushort)result.Anagrams.Count; } result.Anagrams.Add(currentWordAnagrams); } } return(result); }