This practice workspace is built for desktop. Open it on a larger screen to start solving.
Word Ladder
Given a beginWord, an endWord, and a wordList (array of strings), find the length of the shortest transformation sequence from beginWord to endWord, where each step changes exactly one letter and every intermediate word must exist in wordList.
Return the length of the shortest transformation sequence, or 0 if no such sequence exists.
The sequence length counts all words including beginWord and endWord. For example, the sequence hit → hot → dot → dog → cog has length 5.
Approach. Treat each word as a graph node. Two words share an edge if they differ by exactly one character. BFS from beginWord, using a visited set to avoid revisiting. The BFS level (starting at 1 for beginWord) equals the sequence length when endWord is reached.
Constraint: beginWord is not in wordList. endWord must be in wordList for a valid path to exist.
Examples
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.
- beginWord is not in wordList.
Preview Mode
This is a read-only preview of DSAMind's AI coaching. Sign up to get custom feedback on your own solution.
Your approach
- Pattern:Breadth-First Search
- Time:O(N)
- Space:O(1)
Complete approach — pattern and complexity both named.
Coach
What you got right
You correctly matched the sorted input requirement and implemented a linear-time scan using two pointers.
Where it diverged
No divergence detected. The code correctly aligns with the stated approach.
Next attempt: focus on
Try solving related sliding window problems to build familiarity with two-pointer variants.
Signals
No signals fired — clean run, you stayed in flow.