leetcode - 127. Word Ladder

Description

A transformation sequence from word beginWord to word endWord using a dictionary wordList is a sequence of words beginWord -> s1 -> s2 -> ... -> sk such that:

Every adjacent pair of words differs by a single letter.

Every si for 1 <= i <= k is in wordList. Note that beginWord does not need to be in wordList.

sk == endWord

Given two words, beginWord and endWord, and a dictionary wordList, return the number of words in the shortest transformation sequence from beginWord to endWord, or 0 if no such sequence exists.

Example 1:

复制代码
Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]
Output: 5
Explanation: One shortest transformation sequence is "hit" -> "hot" -> "dot" -> "dog" -> cog", which is 5 words long.

Example 2:

复制代码
Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"]
Output: 0
Explanation: The endWord "cog" is not in wordList, therefore there is no valid transformation sequence.

Constraints:

复制代码
1 <= beginWord.length <= 10
endWord.length == beginWord.length
1 <= wordList.length <= 5000
wordList[i].length == beginWord.length
beginWord, endWord, and wordList[i] consist of lowercase English letters.
beginWord != endWord
All the words in wordList are unique.

Solution

BFS, start with endWord, every time change one character to decide if we want to add this to the queue.

Time complexity: o ( n ∗ n ∗ w o r d . l e n + n ) o(n*n*word.len + n) o(n∗n∗word.len+n), where n is the length of wordList, word.len is the length of each word in wordList

Space complexity: o ( n ) o(n) o(n)

Code

python3 复制代码
class Solution:
    def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:
        queue = collections.deque([(endWord, 1)])
        visited = set()
        wordList = set(wordList)
        if endWord not in wordList:
            return 0
        while queue:
            cur_word, step = queue.popleft()
            if cur_word in visited:
                continue
            visited.add(cur_word)
            if cur_word == beginWord:
                return step
            for i in range(len(cur_word)):
                for new_char in 'abcdefghijklmnopqrstuvwxyz':
                    if new_char == cur_word[i]:
                        continue
                    new_word = f'{cur_word[:i]}{new_char}{cur_word[i+1:]}'
                    if new_word in wordList or new_word == beginWord:
                        queue.append((new_word, step + 1))
        return 0
相关推荐
wearegogog12316 分钟前
C# Modbus 协议实现
开发语言·c#
啊哦呃咦唔鱼39 分钟前
LeetCode hot100-3 无重复字符的最长子串
算法·leetcode·职场和发展
bugcome_com1 小时前
C# 泛型(Generic)完全指南:从基础到高级应用
c#
一叶落4381 小时前
LeetCode 21. 合并两个有序链表(C语言详解 | 链表经典题)
c语言·数据结构·c++·算法·leetcode·链表
我是唐青枫2 小时前
C#.NET Memory 深入解析:跨异步边界的内存视图与高性能实战
开发语言·c#·.net
阿里嘎多哈基米2 小时前
速通Hot100-Day04——哈希
数据结构·算法·leetcode·哈希算法·散列表
人工智能AI技术2 小时前
AI Gateway 实战:基于 C# 与 YARP 构建多模型统一接入与路由网关
人工智能·c#
xsyaaaan2 小时前
leetcode-hot100-普通数组:53最大子数组和-56合并区间-189轮转数组-238除了自身以外数组的乘积-41缺失的第一个正数
leetcode
客卿1233 小时前
力扣--组合,子集--回溯法的再探索--总结回溯法
java·算法·leetcode
_日拱一卒3 小时前
LeetCode(力扣):环形链表
算法·leetcode·链表