Beispiel #1
0
        // Insert a child character node and return it.
        public CharNode InsertCharInChild(char value)
        {
            CharNode newNode = FindCharInChild(value);

            if (newNode == null)
            {
                newNode = new CharNode(value);
                this._children.Add(newNode);
            }
            return(newNode);
        }
Beispiel #2
0
        // Break down the word in tree of characters
        public void CompileWord(string word)
        {
            CharNode currentNode = Root;

            foreach (var ch in word)
            {
                currentNode = currentNode.InsertCharInChild(ch);
            }

            // Mark last character node as word
            currentNode.UpdateAsWord(true);
        }
Beispiel #3
0
        public bool FindAnagram(string anagramHash, string inputPhrase, WordTree wordTree, CharNode currentNode, string anagram, int levels)
        {
            string anagramSoFar         = anagram;
            string remainingInputPhrase = inputPhrase;
            // See if we can use the character
            var charIndex = inputPhrase.IndexOf(currentNode.Value);

            if (charIndex != -1)
            {
                anagramSoFar        += currentNode.Value;
                remainingInputPhrase = inputPhrase.Remove(charIndex, 1);
            }
            else if (currentNode != wordTree.Root)
            {
                // oops this character doesn't exist in phrase, no point in going further to children
                return(false);
            }

            // We have covered every character of input phrase
            if (remainingInputPhrase.Length == 0)
            {
                // And we have reached the rabit hole depth, and this current node represents a word
                if (levels == RabitHoleDepth && currentNode.IsWord)
                {
                    Console.Write("\rScanning anagrams: {0} ", anagramSoFar);

                    var computeHash = CreateMD5(anagramSoFar);
                    // See if this is it, if found lets roll
                    if (computeHash.Equals(anagramHash))
                    {
                        Console.WriteLine("matches the hash !!!");
                        this.SaveMessage(anagramHash, anagramSoFar);
                        return(true);
                    }
                }

                // keep looking
                return(false);
            }

            // Continue looking in children
            foreach (CharNode childNode in currentNode.Children)
            {
                if (FindAnagram(anagramHash, remainingInputPhrase, wordTree, childNode, anagramSoFar, levels))
                {
                    return(true);
                }
            }

            // We have a word, lets start over from the root
            if (currentNode.IsWord && levels < RabitHoleDepth)
            {
                if (FindAnagram(anagramHash, remainingInputPhrase, wordTree, wordTree.Root, anagramSoFar + " ", ++levels))
                {
                    return(true);
                }
            }

            return(false);
        }
Beispiel #4
0
 public WordTree()
 {
     _root = new CharNode(' ');
 }