Description
Given two words (beginWord and endWord), and a dictionary’s word list, find the length of shortest transformation sequence from beginWord to endWord, such that:
- Only one letter can be changed at a time.
- Each transformed word must exist in the word list. Note that beginWord is not a transformed word.
Note:
- Return 0 if there is no such transformation sequence.
- All words have the same length.
- All words contain only lowercase alphabetic characters.
- You may assume no duplicates in the word list.
- You may assume beginWord and endWord are non-empty and are not the same.
Example 1:
1 | Input: |
Example 2:
1 | Input: |
解法
这题一看我开始是懵的,看网上的思路发现是一个BFS的题,即每次遍历26个字母替换的所有情况,如果在list里则入队,同时统计更改的步数,步数采用HashMap存储。
具体代码如下:
1 | class Solution { |