5/37 cases passed. Wrong case for "a" "c" ["a","b","c"]. Really can't understand why, works perfectly in VS2015 (for this case as well)


  • 0
    A
    public class Solution
          {
              int length = -1;
              public int LadderLength(string beginWord, string endWord, ISet<string> wordList)
              {
                  Queue<string> toVisit = new Queue<string>();
                  string currentNode = beginWord;
                  string endNode = endWord;
                  toVisit.Enqueue(currentNode);
    
                  while (toVisit.Count > 0)
                  {
                      length++;
                      currentNode = toVisit.Dequeue();
                      wordList.Remove(currentNode);
                      if (currentNode == endWord)
                          return length;
                      ISet<string> neigbours = FindNeighbours(currentNode, wordList);
                      foreach (string neighbour in neigbours)
                      {
                          toVisit.Enqueue(neighbour);
                      }
                  }
                  return 0;
    
              }
    
    
              ISet<string> FindNeighbours(string node, ISet<string> wordList)
              {
                  HashSet<string> neighbours = new HashSet<string>();
                  
                  foreach (string word in wordList)
                  {
                      int notMatching = 0;
                      for (int i = 0; i < node.Length; i++)
                      {
                          if (node[i] != word[i])
                              notMatching++;
                      }
                      if (notMatching == 1)
                          neighbours.Add(word);
                  }
                  return neighbours;
              }
          }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.