Start learning free

LESSON 362 OF 410 · GRAPHS

Word Ladder - Python Solution and Time Complexity Analysis

Practice O(N2 * L) Read the full explanation

Given two words (beginWord and endWord), and a list of words (words), find the length of shortest transformation sequence from beginWord to endWord, such that:

  1. Only one letter can be changed at a time.
  2. Each transformed word must exist in the word list.

Note:

  • Return 0 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.

Example 1:

Input:
beginWord = "hit",
endWord = "cog",
words = ["hot","dot","dog","lot","log","cog"]

Output: 5

Explanation: As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length 5.

Example 2:

Input:
beginWord = "hit"
endWord = "cog"
words = ["hot","dot","dog","lot","log"]

Output: 0

Explanation: The endWord "cog" is not in words, therefore no possible transformation.
STEP 1 OF 16
    Ctrl + Enter to check

    PYTHON · GRAPHS

    Word Ladder in Python, explained

    Understanding the Problem

    The core challenge of this problem is to find the shortest path from the beginWord to the endWord by changing only one letter at a time, with each intermediate word existing in the given word list. This problem is significant in various applications such as spell checkers, word games, and natural language processing tasks.

    Potential pitfalls include not considering all possible transformations or not efficiently searching through the word list, leading to suboptimal solutions.

    Approach

    To solve this problem, we can use the Breadth-First Search (BFS) algorithm. BFS is ideal for finding the shortest path in an unweighted graph, which aligns with our need to find the shortest transformation sequence.

    Here’s a step-by-step approach:

    1. First, check if the endWord is in the word list. If not, return 0 immediately.
    2. Use a queue to perform BFS. Initialize the queue with the beginWord and a depth of 1.
    3. Use a set to keep track of visited words to avoid cycles.
    4. For each word in the queue, generate all possible transformations by changing one letter at a time.
    5. If a transformation matches the endWord, return the current depth + 1.
    6. If the transformation exists in the word list and hasn’t been visited, add it to the queue and mark it as visited.
    7. Continue the process until the queue is empty.

    Algorithm

    Here’s a detailed breakdown of the BFS algorithm:

    1. Initialize the queue with the beginWord and a depth of 1.
    2. While the queue is not empty, do the following:
      1. Dequeue the front element (current word and depth).
      2. For each character in the current word, try changing it to every letter from 'a' to 'z'.
      3. If the new word matches the endWord, return the current depth + 1.
      4. If the new word is in the word list and hasn’t been visited, enqueue it with depth + 1 and mark it as visited.
    3. If the queue is empty and no transformation sequence is found, return 0.

    Code Implementation

    from collections import deque
    
    def ladderLength(beginWord, endWord, wordList):
        # Convert wordList to a set for O(1) lookups
        wordSet = set(wordList)
        if endWord not in wordSet:
            return 0
        
        # Initialize the queue for BFS
        queue = deque([(beginWord, 1)])
        visited = set()
        visited.add(beginWord)
        
        while queue:
            current_word, depth = queue.popleft()
            
            # Try changing each character of the current word
            for i in range(len(current_word)):
                for c in 'abcdefghijklmnopqrstuvwxyz':
                    next_word = current_word[:i] + c + current_word[i+1:]
                    
                    # If the next word is the end word, return the depth + 1
                    if next_word == endWord:
                        return depth + 1
                    
                    # If the next word is in the word set and not visited
                    if next_word in wordSet and next_word not in visited:
                        queue.append((next_word, depth + 1))
                        visited.add(next_word)
        
        return 0
    

    Complexity Analysis

    The time complexity of this BFS approach is O(M^2 * N), where M is the length of each word and N is the number of words in the word list. This is because for each word, we are generating M possible transformations, and for each transformation, we are checking against the word list of size N.

    The space complexity is O(M * N) due to the queue and the visited set.

    Edge Cases

    Consider the following edge cases:

    • If the endWord is not in the word list, the function should return 0.
    • If the beginWord is the same as the endWord, the function should return 1.
    • If the word list is empty, the function should return 0.

    These edge cases are handled in the initial checks of the algorithm.

    Testing

    To test the solution comprehensively, consider the following test cases:

    def test_ladderLength():
        assert ladderLength("hit", "cog", ["hot","dot","dog","lot","log","cog"]) == 5
        assert ladderLength("hit", "cog", ["hot","dot","dog","lot","log"]) == 0
        assert ladderLength("hit", "hit", ["hot","dot","dog","lot","log","cog"]) == 1
        assert ladderLength("hit", "cog", []) == 0
        print("All test cases pass")
    
    test_ladderLength()
    

    Thinking and Problem-Solving Tips

    When approaching such problems, consider the following tips:

    • Break down the problem into smaller, manageable parts.
    • Think about the most efficient data structures to use (e.g., sets for O(1) lookups).
    • Consider edge cases and how to handle them early in your solution.
    • Practice similar problems to improve your problem-solving skills.

    Conclusion

    In this blog post, we discussed the Word Ladder problem, explored a BFS approach to solve it, and analyzed its complexity. Understanding and solving such problems is crucial for improving algorithmic thinking and problem-solving skills. Keep practicing and exploring further to master these concepts.

    Additional Resources

    For further reading and practice, consider the following resources:

    Keep going with PythonAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free