Skip to main content

Word ladder solution

Problem Description​

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

Every adjacent pair of words differs by a single letter. Every si for 1<=i<=k1 <= 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.

Examples​

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<=101 <= beginWord.length <= 10
  • endWord.length==beginWord.lengthendWord.length == beginWord.length
  • 1<=wordList.length<=50001 <= wordList.length <= 5000
  • wordList[i].length==beginWord.lengthwordList[i].length == beginWord.length
  • beginWord, endWord, and wordList[i]wordList[i] consist of lowercase English letters.
  • beginWord!=endWordbeginWord != endWord
  • All the words in wordList are unique.

Solution for Word Ladder II Problem​

Approach :​

Intuition​

We can think of this problem as a graph problem, where each word in the wordList is a node, and there is an edge between two words if they differ by only one character. Our goal is to find the shortest transformation sequence from the beginWord to the endWord.

To solve this problem, we can use a breadth-first search (BFS) approach. We start from the beginWord and explore all its neighboring words (words that differ by only one character). We then explore the neighbors of these neighbors, and so on, until we find the endWord.

During the BFS traversal, we maintain a queue to keep track of the words we need to visit next, and a set to keep track of the words we have already visited. We also keep track of the level of the BFS traversal, which represents the length of the transformation sequence.

Whenever we encounter the endWord during the BFS traversal, we return its level, which represents the length of the shortest transformation sequence. If we are unable to reach the endWord, we return 0.

Overall, the BFS approach allows us to efficiently explore all possible transformation sequences and find the shortest one.

Code in Different Languages​

Written by @mahek0620
 from collections import deque

class Solution:
def ladderLength(self, beginWord, endWord, wordList):
wordSet = set(wordList)
if endWord not in wordSet:
return 0

queue = deque([(beginWord, 1)])

while queue:
word, level = queue.popleft()
if word == endWord:
return level

for i in range(len(word)):
for char in 'abcdefghijklmnopqrstuvwxyz':
next_word = word[:i] + char + word[i+1:]
if next_word in wordSet:
queue.append((next_word, level + 1))
wordSet.remove(next_word)

return 0


References​