private IEnumerable<string> InternalGetAnagrams(Dictionary<char, int> tiles, List<string> path, Node root, int minLength, int minWordLength) { if (final && depth >= minWordLength) { var word = string.Join("", path); var length = word.Replace(" ", "").Length; if (length >= minLength) yield return word; using (new Guard(() => path.Push(" "), path.Pop)) { foreach (var anagram in root.InternalGetAnagrams(tiles, path, root, minLength, minWordLength)) yield return anagram; } } foreach (var child in children) { var l = child.Key; var count = tiles.Get(l); if (count == 0) continue; using (new Guard(() => tiles[l] = count - 1, () => tiles[l] = count)) using (new Guard(() => path.Push(l.ToString()), path.Pop)) { var node = child.Value; foreach (var anagram in node.InternalGetAnagrams(tiles, path, root, minLength, minWordLength)) yield return anagram; } } }
private static Node LoadDictionary(string wordlist) { var result = new Node(); wordlist .Split(new[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries) .Select(word => word.Trim().ToLowerInvariant()) .ToList() .ForEach(result.Add); return result; }
private void MainForm_Load(object sender, EventArgs e) { dictionary = LoadDictionary(Resources.WordsList); }