Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) 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 an empty list 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.
Input:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]
Output: []
Explanation: The endWord "cog" is not in wordList, therefore no possible transformation.
classSolution {publicList<List<String>> findLadders(String beginWord,String endWord,List<String> wordList) {List<List<String>> res =newArrayList<>();Queue<List<String>> queue =newLinkedList<>();Set<String> dict =newHashSet<>(wordList);Set<String> visited =newHashSet<>(Arrays.asList(beginWord));queue.offer(Arrays.asList(beginWord));// Last round will be toggled when we hit one one of the shortest pathboolean lastRound =false;while (!queue.isEmpty() &&!lastRound) {Set<String> nextVisited =newHashSet<>(visited);for (int i =queue.size(); i >0; i--) {List<String> curList =queue.poll();String curW =curList.get(curList.size() -1);if (curW.equals(endWord)) {res.add(curList); lastRound =true;continue; }for (int j =0; j <curW.length(); j++) {char[] next =curW.toCharArray();for (char k ='a'; k <='z'; k++) {if (next[j] == k)continue; next[j] = k;String cand =String.valueOf(next);if (dict.contains(cand) &&!visited.contains(cand)) {List<String> candList =newArrayList<>(curList);nextVisited.add(cand);candList.add(cand);queue.offer(candList); } } } } visited = nextVisited; }return res; }}