public void SolveLadder_WhenWordDictionary_HasNoWords_ReturnsEmptyList()
        {
            // Arrange
            var beginWord           = "HARD";
            var targetWord          = "BOIL";
            var wordDic             = new Dictionary <string, bool>();
            var wordDicPreProcessed = new Dictionary <string, ICollection <string> >();
            var wordsFound          = new HashSet <string>();

            _getWordFromProcessedListService.GetSimiliarWords(Arg.Any <string>(),
                                                              Arg.Any <IDictionary <string, ICollection <string> > >()).Returns(wordsFound);

            var expectedResult = Enumerable.Empty <string>();

            // Act
            var result = _sut.SolveLadder(beginWord, targetWord, wordDic, wordDicPreProcessed);

            // Assert
            result.Should().BeEquivalentTo(expectedResult);
        }
Beispiel #2
0
        private void Solve(string firstWord)
        {
            var queue = new Queue <Word>();

            queue.Enqueue(_root);

            while (queue.Count > 0)
            {
                var newWord = queue.Dequeue();

                var parentWord = newWord.WordKey;

                var similarWords = _getWordFromProcessedListService.GetSimiliarWords(parentWord, _wildCardsdict);
                if (!similarWords.Any())
                {
                    continue;
                }

                foreach (var wordFound in similarWords)
                {
                    if (_dict[wordFound])
                    {
                        continue;
                    }

                    _dict[wordFound] = true;

                    var newWordFound = new Word(wordFound, newWord);
                    queue.Enqueue(newWordFound);

                    if (wordFound.Equals(firstWord))
                    {
                        _target = newWordFound;
                        return;
                    }
                }
            }
        }