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:

1. Only one letter can be changed at a time.
2. Each transformed word must exist in the word list. Note that beginWord is not a transformed word.

For example,

Given:
beginWord = `"hit"`
endWord = `"cog"`
wordList = `["hot","dot","dog","lot","log","cog"]`

As one shortest transformation is `"hit" -> "hot" -> "dot" -> "dog" -> "cog"`,
return its length `5`.

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.

Time Complexity: O(m*n). m = wordList.size(). n = beginWord.length().

Space: O(m*n). queue把所有的word都加了进去.

AC Java:

``` 1 class Solution {
2     public int ladderLength(String beginWord, String endWord, List<String> wordList) {
3         if(wordList == null || wordList.size() == 0){
4             return 0;
5         }
6
7         HashSet<String> dic = new HashSet<>();
9
10         if(!dic.contains(endWord)){
11             return 0;
12         }
13
14         int level = 1;
17         while(!que.isEmpty()){
18             int size = que.size();
19
20             while(size-- > 0){
21                 String cur = que.poll();
22                 if(cur.equals(endWord)){
23                     return level;
24                 }
25
26                 for(int i = 0; i < cur.length(); i++){
27                     char [] arr = cur.toCharArray();
28                     char ori = arr[i];
29                     for(char k = 'a'; k <= 'z'; k++){
30                         if(ori == k){
31                             continue;
32                         }
33
34                         arr[i] = k;
35                         String can = new String(arr);
36                         if(dic.contains(can)){
37                             dic.remove(can);
39                         }
40                     }
41                 }
42             }
43
44             level++;
45         }
46
47         return 0;
48     }
49 }```

Could optimize with bidirectional BFS.

Time Complexity: O(m * n). m = wordList.size(). n = word.length().

Space: O(m * n).

AC Java:

``` 1 class Solution {
2     public int ladderLength(String beginWord, String endWord, List<String> wordList) {
3         if(wordList == null || wordList.size() == 0){
4             return 0;
5         }
6
7         HashSet<String> dic = new HashSet<>();
9
10         if(!dic.contains(endWord)){
11             return 0;
12         }
13
14         HashSet<String> startSet = new HashSet<String>();
16         HashSet<String> endSet = new HashSet<String>();
18         HashSet<String> visited = new HashSet<String>();
20
21         int level = 1;
22         while(!startSet.isEmpty()){
23             if(startSet.size() > endSet.size()){
24                 HashSet<String> temp = startSet;
25                 startSet = endSet;
26                 endSet = temp;
27             }
28
29             HashSet<String> nextSet = new HashSet<String>();
30             for(String cur : startSet){
31                 for(int i = 0; i < cur.length(); i++){
32                     char [] arr = cur.toCharArray();
33                     char ori = arr[i];
34                     for(char k = 'a'; k <= 'z'; k++){
35                         if(k == ori){
36                             continue;
37                         }
38
39                         arr[i] = k;
40                         String can = new String(arr);
41                         if(endSet.contains(can)){
42                             return level + 1;
43                         }
44
47                         }
48                     }
49                 }
50             }
51
52             startSet = nextSet;
53             level++;
54         }
55
56         return 0;
57     }
58 }```

Reference: javascript:void(0)