Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 74 additions & 88 deletions #126
Original file line number Diff line number Diff line change
@@ -1,89 +1,75 @@
126. Word Ladder II

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.
For example,

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

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


public class Solution {

public List<List<String>> findLadders(String beginWord, String endWord, List<String> wordList) {
Map<String, Integer> map = new HashMap<>();
Map<String, Set<String>> neighbors = new HashMap<>();
neighbors.put(beginWord, new HashSet<>());
map.put(beginWord, -1);
Set<String> wordSet = new HashSet<>();
for(String word : wordList){
wordSet.add(word);
map.put(word, -1);
neighbors.put(word, new HashSet<>());
}
int distance = 0;
boolean found = false;
Queue<String> queue = new LinkedList<>();
queue.offer(beginWord);
while(!queue.isEmpty()){
int len = queue.size();
for(int i = 0; i < len; i++){
String curWord = queue.poll();
if(map.get(curWord) == -1 || map.get(curWord) > distance){
map.put(curWord, distance);
}
if(curWord.equals(endWord)){
found = true;
}
for(int j = 0; j < curWord.length(); j++){
char[] arr = curWord.toCharArray();
for(char c = 'a'; c <= 'z'; c++){
if(c != arr[j]){
arr[j] = c;
}
String newWord = new String(arr);
if(wordSet.contains(newWord)){
if(map.get(newWord) == -1){
queue.offer(newWord);
neighbors.get(curWord).add(newWord);
}
}
}
}
}
if(found) break;
distance++;
}
List<List<String>> wraplist = new ArrayList<>();
dfs(map, neighbors, new ArrayList<>(), wraplist, beginWord, endWord);
return wraplist;
}

public void dfs(Map<String, Integer> map, Map<String, Set<String>> neighbors, List<String> sublist, List<List<String>> wraplist, String curWord, String endWord){

if(curWord.equals(endWord)){
sublist.add(curWord);
wraplist.add(new ArrayList<>(sublist));
sublist.remove(sublist.size() - 1);
return;
}

for(String neighbor : neighbors.get(curWord)){
sublist.add(curWord);
if(map.get(neighbor) == map.get(curWord) + 1){
dfs(map, neighbors, sublist, wraplist, neighbor, endWord);
}
sublist.remove(sublist.size() - 1);
}
}
public class Solution {

public List<List<String>> findLadders(String beginWord, String endWord, List<String> wordList) {
Set<String> wordDict = new HashSet<>(wordList);
Map<String, Integer> distances = new HashMap<>();
Map<String, Set<String>> graph = new HashMap<>();

wordDict.add(beginWord);
for(String word : wordDict){
graph.put(word, new HashSet<>());
}

Queue<String> queue = new LinkedList<>();
queue.offer(beginWord);
distances.put(beginWord, 0);
boolean found = false;

while(!queue.isEmpty()){
String curWord = queue.poll();
int cur_distance = distances.get(curWord);
Set<String> neighbors = getNeighbors(curWord, wordDict);
for(String neighbor : neighbors){
graph.get(curWord).add(neighbor);
if(!distances.containsKey(neighbor)){
distances.put(neighbor, cur_distance + 1);
queue.offer(neighbor);
}
}
if(curWord.equals(endWord)){
found = true;
break;
}
}

List<List<String>> wraplist = new ArrayList<>();
if(!found) return wraplist;
findPath(distances, graph, new ArrayList<>(), wraplist, endWord, beginWord);
return wraplist;
}

public Set<String> getNeighbors(String curWord, Set<String> wordSet){
Set<String> neighbors = new HashSet<>();
for(int j = 0; j < curWord.length(); j++){
char[] arr = curWord.toCharArray();
for(char c = 'a'; c <= 'z'; c++){
if(c == arr[j]) continue;
arr[j] = c;
String newWord = new String(arr);
if(wordSet.contains(newWord)){
neighbors.add(newWord);
}
}
}
return neighbors;
}

public void findPath(Map<String, Integer> distances, Map<String, Set<String>> graph, List<String> sublist, List<List<String>> wraplist, String curWord, String startWord){

sublist.add(curWord);
if(curWord.equals(startWord)){
List<String> res = new ArrayList<>(sublist);
Collections.reverse(res);
wraplist.add(res);
sublist.remove(sublist.size() - 1);
return;
}

for(String neighbor : graph.get(curWord)){
if(distances.get(neighbor) == distances.get(curWord) - 1){
findPath(distances, graph, sublist, wraplist, neighbor, startWord);
}
}
sublist.remove(sublist.size() - 1);
}
}