Word Ladder
Hard Graphs
beginWord and endWord are words of lower case letters, both of the same length, and wordList is a list of words of that length too.
A ladder goes from beginWord to endWord one word at a time. Each step changes exactly one letter of the word, and the word it makes must be in wordList. beginWord itself need not be in wordList, but every word after it must be, endWord included.
Return the number of words in the shortest ladder, beginWord and endWord included, or 0 if there is no ladder.
Example 1
cold, cord, card, ward, warm: each word changes one letter of the one before, and all but cold are in the list. That is five words, and none shorter can work, as cold and warm differ in all four letters. cold, cord, corm, worm, warm is another of five.
Example 2
Example 3
Constraints
1 ≤ beginWord.length ≤ 10endWordand every word ofwordListhave as many letters asbeginWord1 ≤ wordList.length ≤ 500- Every word uses only the lower case letters
atoz beginWord ≠ endWord, and the words ofwordListare all different
Solve it here
Write it in Python, Java or C++, run it on your own cases, and submit against hidden tests.
Practice this in an AI round
30 minutes on this problem with an AI agent beside your code, then a report on your solution and how you used the agent.
See the AI roundWatch the explanation
More Graphs
- Number of IslandsAI round · Medium
- Rotting OrangesAI round · Medium
- Is Graph Bipartite?AI round · Medium
- Graph Valid TreeMedium
- Walls and GatesMedium