-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path126.java
More file actions
101 lines (95 loc) · 3.51 KB
/
Copy path126.java
File metadata and controls
101 lines (95 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import java.util.*;
public class Solution {
private List<List<String>> ans = new ArrayList<>();
private HashSet<String> wordSet;
private HashMap<String, Set<String>> successors = new HashMap<>();
private LinkedList<String> list = new LinkedList<>();
public boolean bfs(String beginWord, String endWord){
boolean found = false;
HashSet<String> visited = new HashSet<>();
visited.add(beginWord);
Queue<String> q = new LinkedList<>();
HashSet<String> next = new HashSet<>();
q.offer(beginWord);
while(q.isEmpty() == false){
int size = q.size();
for(int i = 0; i < size; i++){
String curStr = q.poll();
int len = curStr.length();
char[] tmp = curStr.toCharArray();
for(int j = 0; j < len; j++){
char oriChar = tmp[j];
for(char c = 'a'; c <= 'z'; c++){
if(c == oriChar){
continue;
}
tmp[j] = c;
String nxtStr = new String(tmp);
if(wordSet.contains(nxtStr) == true){
if(visited.contains(nxtStr) == false){
if(nxtStr.equals(endWord)){
found = true;
}
q.offer(nxtStr);
if (successors.containsKey(curStr)) {
successors.get(curStr).add(nxtStr);
} else {
Set<String> newSet = new HashSet<>();
newSet.add(nxtStr);
successors.put(curStr, newSet);
}
next.add(nxtStr);
}
}
}
tmp[j] = oriChar;
}
}
if(found == true){
break;
}
visited.addAll(next);
next.clear();
}
return found;
}
public void dfs(String curWord, String endWord){
if(curWord.equals(endWord)){
ans.add(new LinkedList<>(list));
return;
}
if(successors.containsKey(curWord) == false){
return;
}
Set<String> s = successors.get(curWord);
for(String str: s){
list.addLast(str);
dfs(str, endWord);
list.removeLast();
}
}
public List<List<String>> findLadders(String beginWord, String endWord, List<String> wordList) {
wordSet = new HashSet<>(wordList);
if(wordSet.contains(endWord) == false || wordList.size() == 0){
return ans;
}
boolean found = false;
found = bfs(beginWord, endWord);
if(found == false){
return ans;
}
list.addLast(beginWord);
dfs(beginWord, endWord);
return ans;
}
public static void main(String[] args) {
String[] words = {"hot","dog","dot"};
List<String> wordList = new ArrayList<>();
Collections.addAll(wordList, words);
Solution solution = new Solution();
String beginWord = "hot";
String endWord = "dog";
List<List<String>> res = solution.findLadders(beginWord, endWord, wordList);
System.out.println(res);
}
}