方法一:(广度优先搜索算法)
class Solution {
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
int L=beginWord.length();
Map<String, List<String>> allComboDict=new HashMap<String, List<String>>();
wordList.forEach(
word->{
for(int i=0;i<L;i++) {
String new_Word=word.substring(0, i)+"*"+word.substring(i+1,L);
List<String> transmations=allComboDict.getOrDefault(new_Word, new ArrayList<String>());
transmations.add(word);
allComboDict.put(new_Word, transmations);
}
});
// BFS需要的队列
Queue<Pair<String,Integer>> Q=new LinkedList<Pair<String,Integer>>();
Q.add(new Pair<String, Integer>(beginWord, 1));
// 记录是否访问过的集合
Map<String, Boolean> visited=new HashMap<String, Boolean>();
visited.put(beginWord, true);
while(!Q.isEmpty()) {
Pair<String, Integer> node=Q.remove();
String word=node.getKey();
int level=node.getValue();
for(int i=0;i<L;i++) {
String new_word=word.substring(0,i)+"*"+word.substring(i+1,L);
for(String adjacentWord : allComboDict.getOrDefault(new_word, new ArrayList<String>())) {
if(adjacentWord.equals(endWord)) {
return level+1;
}
if(!visited.containsKey(adjacentWord)) {
visited.put(adjacentWord, true);
Q.add(new Pair<String, Integer>(adjacentWord, level+1));
}
}
}
}
return 0;
}
}
方法二:(双向广度优先算法)
class Solution {
public static int visitWordNode(Queue<Pair<String, Integer>> Q,
Map<String, Integer> visited,
Map<String, Integer> othersVisited,
int L,
Map<String, List<String>> allComboDict) {
Pair<String, Integer> node=Q.remove();
String word=node.getKey();
int level=node.getValue();
for(int i=0;i<L;i++) {
String new_word=word.substring(0,i)+"*"+word.substring(i+1,L);
for(String adjacentWord : allComboDict.getOrDefault(new_word, new ArrayList<String>())) {
if(othersVisited.containsKey(adjacentWord)) {
return level+othersVisited.get(adjacentWord);
}
if(!visited.containsKey(adjacentWord)) {
visited.put(adjacentWord, level+1);
Q.add(new Pair<String, Integer>(adjacentWord, level+1));
}
}
}
return -1;
}
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
if(!wordList.contains(endWord)) {
return 0;
}
int L=beginWord.length();
Map<String, List<String>> allComboDict=new HashMap<String, List<String>>();
wordList.forEach(
word->{
for(int i=0;i<L;i++) {
String new_word=word.substring(0,i)+"*"+word.substring(i+1,L);
List<String> transmations=allComboDict.getOrDefault(new_word, new ArrayList<String>());
transmations.add(word);
allComboDict.put(new_word, transmations);
}
});
// 双向BFS需要的队列
Queue<Pair<String, Integer>> Q_begin=new LinkedList<Pair<String,Integer>>();
Queue<Pair<String, Integer>> Q_end=new LinkedList<Pair<String,Integer>>();
Q_begin.add(new Pair<String, Integer>(beginWord, 1));
Q_end.add(new Pair<String, Integer>(endWord, 1));
// 记录遍历过的点
Map<String, Integer> visited_begin=new HashMap<String, Integer>();
Map<String, Integer> visited_end=new HashMap<String, Integer>();
visited_begin.put(beginWord, 1);
visited_end.put(endWord, 1);
while(!Q_begin.isEmpty() && !Q_end.isEmpty()) {
int ans=visitWordNode(Q_begin,visited_begin,visited_end,L,allComboDict);
if(ans>-1) {
return ans;
}
ans=visitWordNode(Q_end, visited_end, visited_begin, L, allComboDict);
if(ans>-1) {
return ans;
}
}
return 0;
}
}