# 给定两个单词（beginWord 和 endWord）和一个字典 wordList，找出所有从 beginWord 到 endWord 的最短转换序列。转换需遵循如下规则：
#
# 每次转换只能改变一个字母。
# 转换过程中的中间单词必须是字典中的单词。
# 说明:
#
# 如果不存在这样的转换序列，返回一个空列表。
# 所有单词具有相同的长度。
# 所有单词只由小写字母组成。
# 字典中不存在重复的单词。
# 你可以假设 beginWord 和 endWord 是非空的，且二者不相同。
# 示例 1:
#
# 输入:
# beginWord = "hit",
# endWord = "cog",
# wordList = ["hot","dot","dog","lot","log","cog"]


class Solution:
    def findLadders(self, beginWord, endWord, wordList):
        from collections import defaultdict
        if endWord not in wordList:
            return []
        forward, backward, wordList, dic = {beginWord}, {endWord}, set(wordList), defaultdict(set)
        flag, letters, length = True, set('qwertyuioplkjhgfdsazxcvbnm'), len(endWord)
        while forward:
            if len(forward) > len(backward):
                forward, backward, flag = backward, forward, not flag
            cur = set()
            wordList -= forward
            for word in forward:
                for idx in range(length):
                    x, y = word[:idx], word[idx + 1:]
                    for letter in letters:
                        temp = x + letter + y
                        if temp in wordList:
                            cur.add(temp)
                            if flag:
                                dic[temp].add(word)
                            else:
                                dic[word].add(temp)
            if cur & backward:
                res = [[endWord]]
                while res[0][0] != beginWord:
                    res = [[x] + y for y in res for x in dic[y[0]]]
                return res
            forward = cur
        return []
