Tutorialspoint
Problem
Solution
Submissions

Word Ladder Problem

Certification: Advanced Level Accuracy: 0% Submissions: 0 Points: 15

Write a C++ program to solve the Word Ladder Problem. A word ladder is a sequence of words where each word differs from the previous word by exactly one letter. 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
  • Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]
  • Output: 5
  • Explanation:
    • Step 1: Create a graph where words are connected if they differ by exactly one letter.
    • Step 2: Perform breadth-first search from beginWord to find the shortest path to endWord.
    • Step 3: The shortest transformation is "hit" → "hot" → "dot" → "dog" → "cog".
    • Step 4: Return the length of this sequence, which is 5.
Example 2
  • Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"]
  • Output: 0
  • Explanation:
    • Step 1: Create a graph where words are connected if they differ by exactly one letter.
    • Step 2: Check if endWord exists in wordList - it doesn't.
    • Step 3: Since endWord is not in wordList, no valid transformation sequence exists.
    • Step 4: Return 0 to indicate no valid sequence exists.
Constraints
  • 1 ≤ beginWord.length ≤ 10
  • endWord.length == beginWord.length
  • 1 ≤ wordList.length ≤ 5000
  • All strings consist of lowercase English letters
  • All words in wordList are unique
  • Time Complexity: O(M^2 * N) where M is the length of each word and N is the total number of words
  • Space Complexity: O(M * N)
QueueSetGoldman SachsZomato
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use a breadth-first search (BFS) approach to find the shortest path
  • Create adjacency rules using character substitutions
  • Keep track of visited words to avoid cycles
  • Use a queue to process words level by level
  • Consider preprocessing the wordList for faster neighbor finding

Steps to solve by this approach:

 Step 1: Convert wordList to a set for O(1) lookups and check if endWord exists in it.
 Step 2: Initialize a BFS queue with the beginWord and set level counter to 1.
 Step 3: For each word in the queue, try changing each character to all 26 lowercase letters.
 Step 4: If a changed word equals endWord, return level+1 (current level plus this transformation).
 Step 5: If a changed word exists in wordSet, add it to the queue and remove it from wordSet to avoid cycles.
 Step 6: Increment level counter after processing all words at the current level.
 Step 7: If queue becomes empty without finding endWord, return 0 (no valid sequence).

Submitted Code :