fork download
  1. from collections import defaultdict
  2.  
  3. def ladderLength(beginWord, endWord, wordList):
  4.  
  5. if endWord not in wordList or not endWord or not beginWord or not wordList:
  6. return 0
  7.  
  8. # Since all words are of same length.
  9. L = len(beginWord)
  10.  
  11. # Dictionary to hold combination of words that can be formed,
  12. # from any given word. By changing one letter at a time.
  13. all_combo_dict = defaultdict(list)
  14. for word in wordList:
  15. for i in range(L):
  16. # Key is the generic word
  17. # Value is a list of words which have the same intermediate generic word.
  18. all_combo_dict[word[:i] + "*" + word[i+1:]].append(word)
  19.  
  20.  
  21. # Queue for BFS
  22. queue = collections.deque([(beginWord, 1)])
  23. # Visited to make sure we don't repeat processing same word.
  24. visited = {beginWord: True}
  25. while queue:
  26. current_word, level = queue.popleft()
  27. for i in range(L):
  28. # Intermediate words for current word
  29. intermediate_word = current_word[:i] + "*" + current_word[i+1:]
  30.  
  31. # Next states are all the words which share the same intermediate state.
  32. for word in all_combo_dict[intermediate_word]:
  33. # If at any point if we find what we are looking for
  34. # i.e. the end word - we can return with the answer.
  35. if word == endWord:
  36. return level + 1
  37. # Otherwise, add it to the BFS Queue. Also mark it visited
  38. if word not in visited:
  39. visited[word] = True
  40. queue.append((word, level + 1))
  41. all_combo_dict[intermediate_word] = []
  42. return 0
  43. print("pool","cool",["pool","cool"])
Compilation error #stdin compilation error #stdout 0s 15240KB
stdin
cool
pool
END
cool pool
compilation info
prog.cpp:8:7: error: invalid preprocessing directive #Since
     # Since all words are of same length.
       ^~~~~
prog.cpp:11:7: error: invalid preprocessing directive #Dictionary
     # Dictionary to hold combination of words that can be formed,
       ^~~~~~~~~~
prog.cpp:12:7: error: invalid preprocessing directive #from
     # from any given word. By changing one letter at a time.
       ^~~~
prog.cpp:16:15: error: invalid preprocessing directive #Key
             # Key is the generic word
               ^~~
prog.cpp:17:15: error: invalid preprocessing directive #Value
             # Value is a list of words which have the same intermediate generic word.
               ^~~~~
prog.cpp:21:7: error: invalid preprocessing directive #Queue
     # Queue for BFS
       ^~~~~
prog.cpp:23:7: error: invalid preprocessing directive #Visited
     # Visited to make sure we don't repeat processing same word.
       ^~~~~~~
prog.cpp:23:34: warning: missing terminating ' character
     # Visited to make sure we don't repeat processing same word.
                                  ^
prog.cpp:28:15: error: invalid preprocessing directive #Intermediate
             # Intermediate words for current word
               ^~~~~~~~~~~~
prog.cpp:31:15: error: invalid preprocessing directive #Next
             # Next states are all the words which share the same intermediate state.
               ^~~~
prog.cpp:33:19: error: invalid preprocessing directive #If
                 # If at any point if we find what we are looking for
                   ^~
prog.cpp:34:19: error: invalid preprocessing directive #i
                 # i.e. the end word - we can return with the answer.
                   ^
prog.cpp:37:19: error: invalid preprocessing directive #Otherwise
                 # Otherwise, add it to the BFS Queue. Also mark it visited
                   ^~~~~~~~~
prog.cpp:1:1: error: ‘from’ does not name a type
 from collections import defaultdict
 ^~~~
prog.cpp:25:5: error: expected unqualified-id before ‘while’
     while queue:
     ^~~~~
stdout
Standard output is empty