The "Word Ladder" problem is a classic sequence transformation challenge. You are given a beginWord, an endWord, and a wordList. You need to find the length of the shortest transformation sequence from the start word to the end word, where only one letter can be changed at a time, and every intermediate word must exist in the provided word list. If no such path exists, you return 0.
This is a quintessential Graph Traversal problem asked by top companies like Amazon, Microsoft, and Google. It tests your ability to model a problem where nodes are words and edges are single-character differences. Finding the "shortest" path in an unweighted graph is a prime candidate for Breadth-First Search (BFS), and this problem evaluates if you can implement BFS efficiently.
The primary pattern is Breadth-First Search (BFS).
Start: hit, End: cog, List: ["hot", "dot", "dog", "lot", "log", "cog"].
hit -> hot (1 change, level 2)hot -> dot, lot (level 3)dot -> dog; lot -> log (level 4)dog -> cog; log -> cog (level 5)
Shortest path length: 5.Master the "Level-order BFS" pattern. For Word Ladder, keeping track of the "level" (distance) is crucial. Also, consider Bidirectional BFS for an extra optimization that can significantly speed up the search in large word lists.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Minimum Genetic Mutation | Medium | Solve | |
| Word Ladder II | Hard | Solve | |
| Open the Lock | Medium | Solve | |
| K-Similar Strings | Hard | Solve | |
| HTML Entity Parser | Medium | Solve |