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()

卡内存用回溯写法

相关推荐
VT.馒头1 小时前
【力扣】2722. 根据 ID 合并两个数组
javascript·算法·leetcode·职场和发展·typescript
执着2591 小时前
力扣hot100 - 108、将有序数组转换为二叉搜索树
算法·leetcode·职场和发展
52Hz1182 小时前
力扣230.二叉搜索树中第k小的元素、199.二叉树的右视图、114.二叉树展开为链表
python·算法·leetcode
苦藤新鸡2 小时前
56.组合总数
数据结构·算法·leetcode
菜鸟233号3 小时前
力扣647 回文子串 java实现
java·数据结构·leetcode·动态规划
LiLiYuan.3 小时前
【Cursor 中找不到LeetCode 插件解决办法】
算法·leetcode·职场和发展
Charlie_lll3 小时前
力扣解题-[3379]转换数组
数据结构·后端·算法·leetcode
TracyCoder1233 小时前
LeetCode Hot100(23/100)——142. 环形链表 II
算法·leetcode·链表
TracyCoder1234 小时前
LeetCode Hot100(28/100)——104. 二叉树的最大深度
算法·leetcode
执着2594 小时前
力扣hot100 - 101、对称二叉树
数据结构·算法·leetcode