​LeetCode刷题实战126:单词接龙 II

简介: 算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !

今天和大家聊的问题叫做 单词接龙 II,我们先来看题面:https://leetcode-cn.com/problems/word-ladder-ii/

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.


题意



给定两个单词(beginWord 和 endWord)和一个字典 wordList,找出所有从 beginWord 到 endWord 的最短转换序列。转换需遵循如下规则:


每次转换只能改变一个字母。

转换后得到的单词必须是字典中的单词。


说明:

如果不存在这样的转换序列,返回一个空列表。

所有单词具有相同的长度。

所有单词只由小写字母组成。

字典中不存在重复的单词。

你可以假设 beginWord 和 endWord 是非空的,且二者不相同。


样例

示例 1:
输入:
beginWord = "hit",
endWord = "cog",
wordList = ["hot","dot","dog","lot","log","cog"]
输出:
[
  ["hit","hot","dot","dog","cog"],
  ["hit","hot","lot","log","cog"]
]
示例 2:
输入:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]
输出: []
解释: endWord "cog" 不在字典中,所以不存在符合要求的转换序列。

解题


先利用BFS从endWord到beginWord得到满足连通要求的单词到endWord的最短距离dist。

再利用DFS遍历从beginWord到endWord的得到满足dist的所有单词序列。(注:这里DFS利用递归来做,不能用栈,因为要得到所有满足要求的单词序列)

class Solution:
    def findLadders(self, beginWord: str, endWord: str, wordList: List[str]) -> List[List[str]]:
        wordList=set(wordList)
        wordList.add(beginWord)
        #wordList.append(beginWord)
        dist=self.bfs(endWord,wordList)
        res=[]
        self.dfs(beginWord,endWord,wordList,dist,[beginWord],res)
        return res
    #bfs记录点到终点的最短距离:end -> start
    def bfs(self,begin,wordList):
        dist={}
        dist[begin]=0
        queue=[begin]
        while queue:
            L=len(queue)
            for _ in range(L):
                word=queue.pop(0)
                for w in self.nextWord(word,wordList):
                    if w not in dist:
                        dist[w]=dist[word]+1
                        queue.append(w)
        return dist
    #利用递归来记录路径:从start -> end
    def dfs(self,curr,end,wordList,dist,path,res):
        if curr==end:
            res.append(list(path))
        for w in self.nextWord(curr,wordList):
            if dist[w]==dist[curr]-1:
                path.append(w)
                self.dfs(w,end,wordList,dist,path,res)
                path.pop()
    def nextWord(self,word,wordList):
        res=[]
        for i in range(len(word)):
            for letter in "abcdefghijklmnopqrstuvwxyz":
                next_=word[0:i]+letter+word[i+1::]
                if next_!=word and next_ in wordList:
                    res.append(next_)
        return res

好了,今天的文章就到这里,如果觉得有所收获,请顺手点个在看或者转发吧,你们的支持是我最大的动力。

相关文章
|
15天前
|
算法 C++
【数据结构与算法】:关于时间复杂度与空间复杂度的计算(C/C++篇)——含Leetcode刷题-2
【数据结构与算法】:关于时间复杂度与空间复杂度的计算(C/C++篇)——含Leetcode刷题
|
15天前
|
算法 C++
【数据结构与算法】:关于时间复杂度与空间复杂度的计算(C/C++篇)——含Leetcode刷题-1
【数据结构与算法】:关于时间复杂度与空间复杂度的计算(C/C++篇)——含Leetcode刷题
|
16天前
|
索引
【LeetCode刷题】二分查找:山脉数组的峰顶索引、寻找峰值
【LeetCode刷题】二分查找:山脉数组的峰顶索引、寻找峰值
|
16天前
|
算法
【LeetCode刷题】滑动窗口解决问题:串联所有单词的子串(困难)、最小覆盖子串(困难)
【LeetCode刷题】滑动窗口解决问题:串联所有单词的子串(困难)、最小覆盖子串(困难)
|
16天前
|
算法 容器
【LeetCode刷题】滑动窗口解决问题:水果成篮、找到字符串中所有字母异位词
【LeetCode刷题】滑动窗口解决问题:水果成篮、找到字符串中所有字母异位词
|
16天前
【LeetCode刷题】专题三:二分查找模板
【LeetCode刷题】专题三:二分查找模板
【LeetCode刷题】专题三:二分查找模板
|
2天前
|
算法 测试技术 索引
力扣经典150题第三十二题:串联所有单词的子串
力扣经典150题第三十二题:串联所有单词的子串
4 0
|
2天前
|
算法
力扣经典150题第二十一题:反转字符串中的单词
力扣经典150题第二十一题:反转字符串中的单词
6 0
|
2天前
|
算法
力扣经典150题第十九题:最后一个单词的长度
力扣经典150题第十九题:最后一个单词的长度
6 0
|
16天前
【LeetCode刷题】前缀和解决问题:742.寻找数组的中心下标、238.除自身以外数组的乘积
【LeetCode刷题】前缀和解决问题:742.寻找数组的中心下标、238.除自身以外数组的乘积