LeetCode link: 127. Word Ladder
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
sifor1 <= i <= kis inwordList. Note thatbeginWorddoes not need to be inwordList. 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.
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.
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.
1 <= beginWord.length <= 10endWord.length == beginWord.length1 <= wordList.length <= 5000wordList[i].length == beginWord.lengthbeginWord,endWord, andwordList[i]consist of lowercase English letters.beginWord != endWord- All the words in
wordListare unique.
This problem is hard. Before solving this problem, you can do the following problem first:
The word transformation sequence problem can be abstracted into a graph theory problem. And it is an undirected graph:
-
As shown in the figure above, breadth-first search can be thought of as visiting vertices in rounds and rounds. Actually, whenever you see a question is about getting
shortestorleastof something of a graph,breadth-first searchwould probably help. -
breadth-first searchemphasizes first-in-first-out, so a queue is needed.
Breadth-First Searcha graph means traversing from near to far, fromcircle 1tocircle N. Eachcircleis a round of iteration, but we can simplify it by using just 1 round.- So through
Breadth-First Search, when a word matchesendWord, the game is over, and we can return the number of circle as a result.
- Time:
O(n * n). - Space:
O(n).
class Solution:
def __init__(self):
self.word_set = None
self.end_word = None
self.queue = deque()
def ladderLength(self, begin_word: str, end_word: str, word_list: List[str]) -> int:
self.end_word = end_word
self.word_set = set(word_list)
if end_word not in self.word_set:
return 0
self.queue.append((begin_word, 1))
return self.breadth_first_search()
def breadth_first_search(self):
while self.queue:
word0, circle = self.queue.popleft()
removed_words = set()
for word in self.word_set:
if one_char_different(word, word0):
if word == self.end_word:
return circle + 1
self.queue.append((word, circle + 1))
removed_words.add(word)
self.word_set -= removed_words
return 0
def one_char_different(word1, word2):
different_char_count = 0
for i in range(len(word1)):
if word1[i] != word2[i]:
different_char_count += 1
if different_char_count > 1:
return False
return True// Welcome to create a PR to complete the code of this language, thanks!// Welcome to create a PR to complete the code of this language, thanks!// Welcome to create a PR to complete the code of this language, thanks!// Welcome to create a PR to complete the code of this language, thanks!// Welcome to create a PR to complete the code of this language, thanks!# Welcome to create a PR to complete the code of this language, thanks!// Welcome to create a PR to complete the code of this language, thanks!// Welcome to create a PR to complete the code of this language, thanks!// Welcome to create a PR to complete the code of this language, thanks!// Welcome to create a PR to complete the code of this language, thanks!// Welcome to create a PR to complete the code of this language, thanks!

