Word Ladder Problem
Word Ladder Problem — ExecCode Hard DSA Practice
Solve the Word Ladder problem on ExecCode. Free online hard DSA practice in Arrays - Basics. Write and run code in Java, C++, Python — no signup required to run.
Problem description
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 differs by a single letter and every si (including sk = endWord) is in wordList. Return the number of words in the shortest transformation sequence from beginWord to endWord, or 0 if no such sequence exists.
Examples
Input beginWord = "hit", endWord = "cog", wordList = ["hot", "dot", "dog", "lot", "log", "cog", "hat", "haa"]; Output 5. Input beginWord = "hit", endWord = "cog", wordList = ["hot", "dot", "dog", "lot", "log"]; Output 0. Input beginWord = "hot", endWord = "dog", wordList = ["dot", "dog"]; Output 3
Constraints
1 ≤ beginWord.length ≤ 10 endWord.length == beginWord.length 1 ≤ wordList.length ≤ 5000 wordList[i].length == beginWord.length beginWord, endWord, and wordList[i] consist of lowercase English letters beginWord != endWord All words in wordList are unique
Practice Word Ladder free on ExecCode. Browse DSA problems, topic map, and placement guides.