Link: https://leetcode.com/problems/word-ladder/
Solution:
Topics: BFS
Intuition
Really great BFS problem. Classic shortest path pattern. Basically, to move to another word in the possible sequence, the word must be both valid and 1 letter different from the previous word (parent).
We choose BFS here because the branching factor can be quite high (many children per parent), and only one branch will lead to the correct sequence. In other words, it does not make sense to fully explore every possible sequence of each branch. Most of those branches will not be fruitful, so its far more efficient to explore all branches at once, in level-by-level order…and by default, we also get the shortest path.
High branching factor:
Not all transformations will be valid, so the branching factor is not so extreme…however we cannot rely on this.
Implementation
Review 1
Solved it almost instantly. One thing to consider is that instead of keeping a visited
set, we can simply remove words from wordList
to achieve the same effect.