Skip to content

Commit b7c02bd

Browse files
solves #127: word ladder in java
1 parent 3d079dd commit b7c02bd

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
| 122 | [Best Time to Buy and Sell Stocks II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii) | [![Java](assets/java.png)](src/BestTimeToBuyAndSellStockII.java) [![Python](assets/python.png)](python/best_time_to_buy_and_sell_stock_ii.py) | |
124124
| 123 | [Binary Tree Maximum Path Sum](https://leetcode.com/problems/binary-tree-maximum-path-sum) | [![Java](assets/java.png)](src/BinaryTreeMaximumPathSum.java) | |
125125
| 125 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome) | [![Java](assets/java.png)](src/ValidPalindrome.java) [![Python](assets/python.png)](python/valid_palindrome.py) | |
126+
| 127 | [Word Ladder](https://leetcode.com/problems/word-ladder) | [![Java](assets/java.png)](src/WordLadder.java) | |
126127
| 128 | [Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence) | [![Java](assets/java.png)](src/LongestConsecutiveSequence.java) | |
127128
| 129 | [Sum Root to Leaf Numbers](https://leetcode.com/problems/sum-root-to-leaf-numbers) | [![Java](assets/java.png)](src/SumRootToLeafNumbers.java) | |
128129
| 130 | [Surrounded Regions](https://leetcode.com/problems/surrounded-regions) | [![Java](assets/java.png)](src/SurroundedRegions.java) | |

Diff for: src/MinimumGeneticMutation.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,25 @@ public static int minMutation(String startGene, String endGene, String[] bank) {
2626
}
2727

2828
final Queue<Info> queue = new LinkedList<>() {{ add(new Info(startGene, 0)); }};
29-
final Map<String, Integer> distances = new HashMap<>();
29+
final Set<String> visited = new HashSet<>();
3030

3131
while (!queue.isEmpty()) {
3232
final Info info = queue.poll();
33-
if (distances.getOrDefault(info.mutation, Integer.MAX_VALUE) <= info.steps) {
33+
if (visited.contains(info.mutation)) {
3434
continue;
3535
}
36-
distances.put(info.mutation, info.steps);
36+
visited.add(info.mutation);
37+
38+
if (endGene.equals(info.mutation)) {
39+
return info.steps;
40+
}
3741

3842
for (String neighbour : validMutations(genePool, info.mutation)) {
3943
queue.add(new Info(neighbour, info.steps + 1));
4044
}
4145
}
4246

43-
return distances.getOrDefault(endGene, -1);
47+
return -1;
4448
}
4549

4650
// T: O(|s|), S: O(|s|)

Diff for: src/WordLadder.java

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// https://leetcode.com/problems/word-ladder
2+
// T: O(nB + n^m * nm)
3+
// S: O(nB + n^m)
4+
5+
import java.util.ArrayList;
6+
import java.util.HashSet;
7+
import java.util.LinkedList;
8+
import java.util.List;
9+
import java.util.Queue;
10+
import java.util.Set;
11+
12+
public class WordLadder {
13+
private record Info(String string, int steps) {}
14+
15+
// n = possible chars, m = |s|
16+
// V = n^m
17+
// BFS, T: O(V + E) = O(nB + n^m * nm), S: O(n^m)
18+
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
19+
final Set<String> words = new HashSet<>(wordList);
20+
if (!words.contains(endWord)) {
21+
return 0;
22+
}
23+
24+
final Queue<Info> queue = new LinkedList<>() {{ add(new Info(beginWord, 0)); }};
25+
final Set<String> visited = new HashSet<>();
26+
27+
while (!queue.isEmpty()) {
28+
final Info info = queue.poll();
29+
if (visited.contains(info.string)) {
30+
continue;
31+
}
32+
visited.add(info.string);
33+
if (endWord.equals(info.string)) {
34+
return info.steps + 1;
35+
}
36+
37+
for (String neighbour : validMutations(words, info.string)) {
38+
queue.add(new Info(neighbour, info.steps + 1));
39+
}
40+
}
41+
42+
return -1;
43+
}
44+
45+
// T: O(n * m), S: O(n * m)
46+
private static List<String> validMutations(Set<String> words, String string) {
47+
final List<String> list = new ArrayList<>();
48+
for (int i = 0 ; i < string.length() ; i++) {
49+
for (char c = 'a' ; c <= 'z' ; c++) {
50+
final String word = string.substring(0, i) + c + string.substring(i + 1);
51+
if (words.contains(word)) {
52+
list.add(word);
53+
}
54+
}
55+
}
56+
return list;
57+
}
58+
}

0 commit comments

Comments
 (0)