Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord, such that:
       Only one letter can be changed at a time
       Each transformed word must exist in the word list. Note that beginWord is not a transformed word.
Note:
       Return an empty list if there is no such transformation sequence.
       All words have the same length.
       All words contain only lowercase alphabetic characters.
       You may assume no duplicates in the word list.
       You may assume beginWord and endWord are non-empty and are not the same.
Example 1:
Input:
beginWord = "hit",
endWord = "cog",
wordList = ["hot","dot","dog","lot","log","cog"]
Output:
[
  ["hit","hot","dot","dog","cog"],
  ["hit","hot","lot","log","cog"]
]
Example 2:
Input:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]
Output: []
Explanation: The endWord "cog" is not in wordList, therefore no possible transformation.
class Solution:
     def findLadders(self, beginWord, endWord, wordList):
         wordDict = set(wordList)
         if endWord not in wordDict: return []
         wordDict.discard(beginWord)
         front, back = set([beginWord]), set([endWord])
         length = 2
         direction = 1
         parents = collections.defaultdict(set)
         
         while front:
             next_level = set()
             for word in front:
                 for index in range(len(beginWord)):
                     p1, p2 = word[:index], word[index+1:]
                     for ch in 'abcdefghijklmnopqrstuvwxyz':
                         if word[index] != ch:
                             next_word = p1 + ch + p2
                             if next_word in wordDict:
                                 next_level.add(next_word)
                                 if direction == 1:
                                     parents[next_word].add(word)
                                 else:
                                     parents[word].add(next_word)
                             
             if next_level & back:
                 res = [[endWord]]
                 while res and res[0][0] !=beginWord:
                     res = [[p]+r for r in res for p in parents[r[0]]]
                 return res
                 
             length += 1
             front = next_level
             if len(front) > len(back):
                 direction *= -1
                 front, back = back, front
             wordDict -= front
         return []
         
         
         
         
         
         
         """
         :type beginWord: str
         :type endWord: str
         :type wordList: List[str]
         :rtype: List[List[str]]
         """

class Solution:
     def findLadders(self, beginWord, endWord, wordList):
         wordDict = set(wordList)
         if endWord not in wordDict: return []
         wordDict.discard(beginWord)
         front, back = set([beginWord]), set([endWord])
         length = 2
         direction = 1
         parents = collections.defaultdict(set)
         
         while front:
             next_level = set()
             for word in front:
                 for index in range(len(beginWord)):
                     p1, p2 = word[:index], word[index+1:]
                     for ch in 'abcdefghijklmnopqrstuvwxyz':
                         if word[index] != ch:
                             next_word = p1 + ch + p2
                             if next_word in wordDict:
                                 next_level.add(next_word)
                                 if direction == 1:
                                     parents[next_word].add(word)
                                 else:
                                     parents[word].add(next_word)
                             
             if next_level & back:
                 res = [[endWord]]
                 while res and res[0][0] !=beginWord:
                     res = [[p]+r for r in res for p in parents[r[0]]]
                 return res
                 
             length += 1
             front = next_level
             if len(front) > len(back):
                 direction *= -1
                 front, back = back, front
             wordDict -= front
         return []
         
         
         
         
         
         
         """
         :type beginWord: str
         :type endWord: str
         :type wordList: List[str]
         :rtype: List[List[str]]
         """
