126. Word Ladder II

126. Word Ladder II

python 复制代码
class Solution:
    def findLadders(self, beginWord: str, endWord: str, wordList: List[str]) -> List[List[str]]:
        wordList=set(wordList)

        res=[]
        layer={beginWord:[[beginWord]]}

        while layer:
            newlayer=defaultdict(list)
            for w in layer:
                if w==endWord:
                    res.extend(k for k in layer[w])
                else:
                    for i in range(len(w)):
                        for c in string.ascii_lowercase:
                            neww=w[:i]+c+w[i+1:]
                            if neww in wordList:
                                newlayer[neww]+=[j+[neww] for j in layer[w]]
                
            wordList-=set(newlayer.keys())
            layer=newlayer
        return res
python 复制代码
class Solution:
    def findLadders(self, beginWord: str, endWord: str, wordList: List[str]) -> List[List[str]]:
        wordList=set(wordList)

        if endWord not in wordList:
            return []

        wordList.add(beginWord)
        wordDict=defaultdict(list)

        for word in wordList:
            for i in range(len(word)):
                pattern = word[:i]+'*'+word[i+1:]
                wordDict[pattern].append(word)
        
        queue=deque([(beginWord,1)])
        visited,adjList=defaultdict(set),defaultdict(list)
        visited[beginWord]=0
        numStep=1

        while queue:
            currWord,step=queue.popleft()

            if currWord==endWord:
                numStep=step
                break
            
            for i in range(len(currWord)):
                pattern=currWord[:i]+'*'+currWord[i+1:]
                if pattern in wordDict:
                    for word in wordDict[pattern]:
                        if word not in visited or visited[word]==step+1:
                            adjList[word].append(currWord)
                        
                            if word not in visited:
                                queue.append((word,step+1))
                                visited[word]=step+1

        if numStep==1:return []
        result=[]
        array=[endWord]

        self.backtrack(result,array, numStep,beginWord,adjList)
        return result

    def backtrack(self,result,array,numStep,beginWord,adjList):
        if len(array)==numStep and array[-1]==beginWord:
            temp=array.copy()
            result.append(temp[::-1])
            return 
        
        for word in adjList[array[-1]]:
            array.append(word)
            self.backtrack(result,array,numStep,beginWord, adjList)
            array.pop()

卡内存用回溯写法

相关推荐
Morwit2 小时前
【力扣hot100】 221. 最大正方形
前端·算法·leetcode
Java成神之路-2 小时前
【LeetCode 刷题笔记】69.x 的平方根 | 二分查找经典刷题题解
算法·leetcode
lcj25113 小时前
【数据结构精讲】堆与二叉树从底层原理到代码落地:堆的构建 / 调整 / 排序 + 二叉树遍历 / 操作(附完整 C++ 源码 + LeetCode 题解)
数据结构·c++·leetcode
Java成神之路-5 小时前
【LeetCode 刷题笔记】367.有效的完全平方数 | 二分查找经典刷题题解
算法·leetcode
Java成神之路-13 小时前
【LeetCode 刷题笔记】34. 在排序数组中查找元素的第一个和最后一个位置 | 二分查找经典刷题题解
算法·leetcode
承渊政道1 天前
【动态规划算法】(完全背包问题从状态定义到空间优化)
数据结构·c++·学习·算法·leetcode·动态规划·哈希算法
超级大福宝1 天前
【力扣48. 旋转图像】超好记忆版 + 口诀
c++·算法·leetcode
人道领域1 天前
【LeetCode刷题日记】掌握二叉树遍历:栈实现的三种绝妙方法
算法·leetcode·职场和发展
阿Y加油吧1 天前
二刷 LeetCode:动态规划经典双题复盘
算法·leetcode·动态规划
莫等闲-1 天前
代码随想录一刷记录Day44——leetcode1143.最长公共子序列 53. 最大子序和
数据结构·c++·算法·leetcode·动态规划