LeetCode 127: Word Ladder Solution

Master LeetCode problem 127 (Word Ladder), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

Problem Explanation

Explanation

To solve this problem, we can use a breadth-first search (BFS) algorithm. We start by creating a graph where each word is a node, and there is an edge between two words if they differ by exactly one character. We then perform a BFS starting from the beginWord, visiting each word in the wordList to find the shortest transformation sequence to reach the endWord.

  • Algorithmic Idea:

    1. Create a set wordSet from the wordList for faster lookup.
    2. Initialize a queue q and add beginWord to the queue along with a level of 1.
    3. While the queue is not empty:
      • Pop a word from the queue.
      • Generate all possible next words by changing one character at a time and check if it exists in wordSet.
      • If the generated word is the endWord, return the level.
      • Otherwise, add the generated word to the queue and remove it from wordSet.
    4. If the queue becomes empty, return 0 as no transformation sequence exists.
  • Time Complexity:

    • Building the graph: O(N * M^2), where N is the number of words in the wordList and M is the length of each word.
    • BFS traversal: O(N * M), where N is the number of words in the wordList and M is the length of each word. Overall, the time complexity is O(N * M^2) + O(N * M) = O(N * M^2).
  • Space Complexity:

    • Additional space for the graph: O(N * M^2)
    • Queue and set: O(N * M) Overall, the space complexity is O(N * M^2).

Solution Code

import java.util.*;

class Solution {
    public int ladderLength(String beginWord, String endWord, List<String> wordList) {
        Set<String> wordSet = new HashSet<>(wordList);
        Queue<String> queue = new LinkedList<>();
        queue.offer(beginWord);
        int level = 1;

        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                String currentWord = queue.poll();
                if (currentWord.equals(endWord)) {
                    return level;
                }

                for (int j = 0; j < currentWord.length(); j++) {
                    char[] charArray = currentWord.toCharArray();
                    for (char ch = 'a'; ch <= 'z'; ch++) {
                        charArray[j] = ch;
                        String newWord = new String(charArray);
                        if (wordSet.contains(newWord) && !newWord.equals(currentWord)) {
                            queue.offer(newWord);
                            wordSet.remove(newWord);
                        }
                    }
                }
            }
            level++;
        }

        return 0;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 127 (Word Ladder)?

This page provides optimized solutions for LeetCode problem 127 (Word Ladder) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 127 (Word Ladder)?

The time complexity for LeetCode 127 (Word Ladder) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 127 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 127 in Java, C++, or Python.

Back to LeetCode Solutions