Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit aa29809

Browse files
edit 127
1 parent 95ea221 commit aa29809

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ Your ideas/fixes/algorithms are more than welcome!
470470
|130|[Surrounded Regions](https://leetcode.com/problems/surrounded-regions/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_130.java)| O(?)|O(?) | Medium|
471471
|129|[Sum Root to Leaf Numbers](https://leetcode.com/problems/sum-root-to-leaf-numbers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_129.java)| O(n)|O(h) | Medium| DFS
472472
|128|[Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_128.java)| O(?)|O(?) | Hard| Union Find
473-
|127|[Word Ladder](https://leetcode.com/problems/word-ladder/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_127.java)| O(?)|O(?) | Medium| BFS
473+
|127|[Word Ladder](https://leetcode.com/problems/word-ladder/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_127.java)| O(n^2)|O(n) | Medium| BFS
474474
|126|[Word Ladder II](https://leetcode.com/problems/word-ladder-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_126.java)| O(?)|O(?) | Hard| BFS
475475
|125|[Valid Palindrome](https://leetcode.com/problems/valid-palindrome/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_125.java)| O(n)|O(1) | Easy| Two Pointers
476476
|124|[Binary Tree Maximum Path Sum](https://leetcode.com/problems/binary-tree-maximum-path-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_124.java)| O(n)|O(h) | Hard | Tree, DFS

src/main/java/com/fishercoder/solutions/_127.java

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,39 +33,53 @@
3333

3434
public class _127 {
3535

36-
/**reference: https://discuss.leetcode.com/topic/29303/two-end-bfs-in-java-31ms/16*/
36+
/**this one https://discuss.leetcode.com/topic/29303/two-end-bfs-in-java-31ms fails by test case _127Test.test1().
37+
* All transformed words, including endWord must be in wordList.
38+
*
39+
* And we can share a visited set from both ends since we cannot remove word from dict.*/
3740
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
38-
Set<String> dict = new HashSet<>(wordList);
39-
Set<String> startSet = new HashSet<>();
41+
Set<String> beginSet = new HashSet<>();
4042
Set<String> endSet = new HashSet<>();
4143
Set<String> visited = new HashSet<>();
44+
Set<String> dict = new HashSet<>(wordList);
45+
int len = 1;
46+
47+
beginSet.add(beginWord);
4248

43-
startSet.add(beginWord);
4449
if (dict.contains(endWord)) {
45-
endSet.add(endWord); // all transformed words must be in dict (including endWord)
50+
endSet.add(endWord);
4651
}
4752

48-
for (int len = 2; !startSet.isEmpty(); len++) {
49-
Set<String> nq = new HashSet<>();
50-
for (String w : startSet) {
51-
for (int j = 0; j < w.length(); j++) {
52-
char[] ch = w.toCharArray();
53+
while (!beginSet.isEmpty() && !endSet.isEmpty()) {
54+
if (beginSet.size() > endSet.size()) {
55+
Set<String> temp = beginSet;
56+
beginSet = endSet;
57+
endSet = temp;
58+
}
59+
60+
Set<String> temp = new HashSet<>();
61+
for (String word : beginSet) {
62+
char[] chars = word.toCharArray();
63+
for (int i = 0; i < chars.length; i++) {
5364
for (char c = 'a'; c <= 'z'; c++) {
54-
if (c == w.charAt(j)) continue; // beginWord and endWord should not be the same
55-
ch[j] = c;
56-
String nb = String.valueOf(ch);
57-
if (endSet.contains(nb)) {
58-
return len; // meet from two ends
65+
char old = chars[i];
66+
chars[i] = c;
67+
String newWord = new String(chars);
68+
if (endSet.contains(newWord)) {
69+
return len + 1;
5970
}
60-
if (dict.contains(nb) && visited.add(nb)) {
61-
nq.add(nb); // not meet yet, visited is safe to use
71+
72+
if (!visited.contains(newWord) && dict.contains(newWord)) {
73+
visited.add(newWord);
74+
temp.add(newWord);
6275
}
76+
chars[i] = old;
6377
}
6478
}
6579
}
6680

67-
startSet = (nq.size() < endSet.size()) ? nq : endSet; // switch to small one to traverse from other end
68-
endSet = (startSet == nq) ? endSet : nq;
81+
beginSet = temp;
82+
len++;
6983
}
7084
return 0;
7185
}

0 commit comments

Comments
 (0)