Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that:

1. Only one letter can be changed at a time
2. Each intermediate word must exist in the dictionary

For example,

Given:
start = `"hit"`
end = `"cog"`
dict = `["hot","dot","dog","lot","log"]`

Return

```  [
["hit","hot","dot","dog","cog"],
["hit","hot","lot","log","cog"]
]
```

Note:

• All words have the same length.
• All words contain only lowercase alphabetic characters.

The solution contains two steps 1 Use BFS to construct a graph. 2. Use DFS to construct the paths from end to start.Both solutions got AC within 1s.

The first step BFS is quite important. I summarized three tricks

1) Using a MAP to store the min ladder of each word, or use a SET to store the words visited in current ladder, when the current ladder was completed, delete the visited words from unvisited. That's why I have two similar solutions.

2) Use Character iteration to find all possible paths. Do not compare one word to all the other words and check if they only differ by one character.

3) One word is allowed to be inserted into the queue only ONCE. See my comments.

The idea is to use bfs build the graph level by level, use Map<String,Set> to store the parents of string, then use dfs to find the path.

``` 1 public class Solution {
2     List<List<String>> result;
3     HashMap<String, Set<String>> wordGraph;
4     public List<List<String>> findLadders(String start, String end, Set<String> dict) {
5         result = new ArrayList<List<String>>();
6         if(dict == null || dict.size() == 0) return result;
7         wordGraph = new HashMap<String, Set<String>>();
8         wordGraph.put(start, new HashSet<String>());
10         HashSet<String> used = new HashSet<String>();
13         boolean flg = false;
14         while(queue.size() != 1){
15             String cur = queue.poll();
17             if(cur == null){
18                 if(flg == true) break;
20             }else{
21                 char[] arr = cur.toCharArray();
22                 for(int i = 0; i < cur.length(); i ++){
23                     for(char j = 'a'; j <= 'z'; j ++){
24                         if(arr[i] == j) continue;
25                         char tmp = arr[i];
26                         arr[i] = j;
27                         String tmpString = String.valueOf(arr);
28                         if(end.equals(tmpString)){
29                             flg = true;
30                             if(!wordGraph.containsKey(tmpString)){
31                                 Set<String> parents = new HashSet<String>();
33                                 wordGraph.put(tmpString, parents);
34                             }else{
36                             }
37                         }else if(dict.contains(tmpString) && !used.contains(tmpString) && flg != true){
38                             if(!wordGraph.containsKey(tmpString)){
39                                 Set<String> parents = new HashSet<String>();
41                                 wordGraph.put(tmpString, parents);
42                             }else{
44                             }
46
47                         }
48                         arr[i] = tmp;
49                     }
50                 }
51             }
52         }
53         if(wordGraph.containsKey(end))
54             findPath(end, start, new ArrayList<String>());
55         return result;
56     }
57
58
59     private void findPath(String cur, String goal, ArrayList<String> row){
60         if(cur.equals(goal)){