"Word Ladder II" is a more complex version of the first problem. Instead of just finding the length of the shortest transformation sequence, you must return all the shortest paths from beginWord to endWord.
This is a "Hard" difficulty problem because it requires combining two different algorithmic techniques: BFS to find the shortest distance and DFS/Backtracking to reconstruct all paths. Companies like Uber and Citadel ask this to see if you can manage complex data structures and avoid redundant computation during path reconstruction.
The pattern is BFS followed by Backtracking.
beginWord.endWord to trace back all paths to the beginWord using the recorded distances or the adjacency list.Start: red, End: tax, List: ["ted", "tex", "tax", "tad"].
red -> ted -> tex -> tax (length 4) and red -> tad -> tax? No, tad to tax is two changes.red -> ted -> tex -> tax and potentially another path like red -> tad -> ted -> tex -> tax.[["red", "ted", "tex", "tax"]]. (Note: in more complex lists, multiple paths of the same minimum length exist).This problem is a great way to practice Layered BFS. Remember that when building the graph for multiple shortest paths, a word in layer i can have multiple parents in layer i-1.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Remove Invalid Parentheses | Hard | Solve | |
| Word Ladder | Hard | Solve | |
| Split a String Into the Max Number of Unique Substrings | Medium | Solve | |
| Minimum Genetic Mutation | Medium | Solve | |
| Word Pattern II | Medium | Solve |