126. Word Ladder II
Input:
beginWord = "hit",
endWord = "cog",
wordList = ["hot","dot","dog","lot","log","cog"]
Output:
[
["hit","hot","dot","dog","cog"],
["hit","hot","lot","log","cog"]
]Last updated
Was this helpful?
Input:
beginWord = "hit",
endWord = "cog",
wordList = ["hot","dot","dog","lot","log","cog"]
Output:
[
["hit","hot","dot","dog","cog"],
["hit","hot","lot","log","cog"]
]Last updated
Was this helpful?
Was this helpful?
Input:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]
Output: []
Explanation: The endWord "cog" is not in wordList, therefore no possible transformation.class Solution(object):
def findLadders(self, beginWord, endWord, wordList):
"""
:type beginWord: str
:type endWord: str
:type wordList: List[str]
:rtype: List[List[str]]
"""
wordList = set(wordList)
res,layer = [], {}
layer[beginWord] = [[beginWord]]
while layer:
newlayer = collections.defaultdict(list)
for w in layer:
if w == endWord:
res += [k for k in layer[w]]
else:
for i in range(len(w)):
for c in string.ascii_lowercase:
neww = w[:i] + c + w[i+1:]
if neww in wordList:
newlayer[neww]+= [j + [neww] for j in layer[w]]
wordList -= set(newlayer.keys()) # for avoiding unsupported operation between sets and list
layer = newlayer
return resclass Solution {
public List<List<String>> findLadders(String beginWord, String endWord, List<String> wordList) {
HashSet<String> dict = new HashSet<>(wordList);
List<List<String>> res = new ArrayList<>();
Map<String, Integer> distance = new HashMap<>();
Map<String, ArrayList<String>> neighbors = new HashMap<>();
ArrayList<String> solution = new ArrayList<>();
dict.add(beginWord);
bfs(beginWord, endWord, dict, distance, neighbors);
dfs(beginWord, endWord, dict, distance, neighbors, solution, res);
return res;
}
private void bfs(String start, String end, Set<String> dict, Map<String, Integer> distance, Map<String, ArrayList<String>> nodeNeighbors) {
for (String str : dict)
nodeNeighbors.put(str, new ArrayList<String>());
Queue<String> queue = new LinkedList<String>();
queue.offer(start);
distance.put(start, 0);
while (!queue.isEmpty()) {
int count = queue.size();
boolean foundEnd = false;
for (int i = 0; i < count; i++) {
String cur = queue.poll();
int curDistance = distance.get(cur);
List<String> neighbors = getNeighbors(cur, dict);
for (String neighbor : neighbors) {
// System.out.println(cur);
nodeNeighbors.get(cur).add(neighbor);
if (!distance.containsKey(neighbor)) {// Check if visited
distance.put(neighbor, curDistance + 1);
if (end.equals(neighbor))// Found the shortest path
foundEnd = true;
else
queue.offer(neighbor);
}
}
}
if (foundEnd)
break;
}
}
private void dfs(String cur, String end, Set<String>dict , Map<String, Integer> distance, Map<String, ArrayList<String>> neighbors, List<String> solution, List<List<String>> res){
solution.add(cur);
// found end: add
if(end.equals(cur)) res.add(new ArrayList<String> (solution));
// not found yet: recursively calling dfs
else{
for(String next: neighbors.get(cur)){
if(distance.get(next) == distance.get(cur) + 1)
dfs(next, end, dict, distance, neighbors, solution, res);
}
}
solution.remove(solution.size() - 1);
}
private List<String> getNeighbors(String node, Set<String> dict){
List<String> res = new ArrayList<>();
char chs [] = node.toCharArray();
for(char ch = 'a'; ch <= 'z'; ch++){
for(int i = 0 ; i < chs.length; i++){
char old = chs[i];
chs[i] = ch;
if(dict.contains(String.valueOf(chs))){
res.add(String.valueOf(chs));
}
chs[i] = old;
}
}
return res;
}
}