Word Ladder
Hard Graphs AI round
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
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.
Your first AI round is free.
Watch the explanation
More Graphs
- Number of IslandsAI round · Medium
- Rotting OrangesAI round · Medium
- Graph Valid TreeMedium
- Walls and GatesMedium
- Number of Connected Components in an Undirected GraphMedium