LeetCode 127. Word Ladder
题目描述:
Given two words (beginWord and endWord), and a dictionary’s word list, find the length of shortest transformation sequence frombeginWord to endWord, such that:
- Only one letter can be changed at a time
- Each intermediate word must exist in the word list
For example,
Given: beginWord =
"hit"
endWord ="cog"
wordList =["hot","dot","dog","lot","log"]
As one shortest transformation is
"hit" -> "hot" -> "dot" -> "dog" -> "cog"
, return its length5
.Note:
- Return 0 if there is no such transformation sequence.
- All words have the same length.
- All words contain only lowercase alphabetic characters.
使用广度优先搜索, 判断是否连接是通过穷举一个单词的所有可能变化来完成的.
1 | class Solution { |