leetcode - 127. Word Ladder

打印 上一主题 下一主题

主题 976|帖子 976|积分 2928

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
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:
  1. Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]
  2. Output: 5
  3. Explanation: One shortest transformation sequence is "hit" -> "hot" -> "dot" -> "dog" -> cog", which is 5 words long.
复制代码
Example 2:
  1. Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"]
  2. Output: 0
  3. Explanation: The endWord "cog" is not in wordList, therefore there is no valid transformation sequence.
复制代码
Constraints:
  1. 1 <= beginWord.length <= 10
  2. endWord.length == beginWord.length
  3. 1 <= wordList.length <= 5000
  4. wordList[i].length == beginWord.length
  5. beginWord, endWord, and wordList[i] consist of lowercase English letters.
  6. beginWord != endWord
  7. 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

  1. class Solution:
  2.     def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:
  3.         queue = collections.deque([(endWord, 1)])
  4.         visited = set()
  5.         wordList = set(wordList)
  6.         if endWord not in wordList:
  7.             return 0
  8.         while queue:
  9.             cur_word, step = queue.popleft()
  10.             if cur_word in visited:
  11.                 continue
  12.             visited.add(cur_word)
  13.             if cur_word == beginWord:
  14.                 return step
  15.             for i in range(len(cur_word)):
  16.                 for new_char in 'abcdefghijklmnopqrstuvwxyz':
  17.                     if new_char == cur_word[i]:
  18.                         continue
  19.                     new_word = f'{cur_word[:i]}{new_char}{cur_word[i+1:]}'
  20.                     if new_word in wordList or new_word == beginWord:
  21.                         queue.append((new_word, step + 1))
  22.         return 0
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

冬雨财经

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表