Skip to content

Commit dffd16f

Browse files
add 433
1 parent 11fa31b commit dffd16f

File tree

3 files changed

+91
-0
lines changed
  • paginated_contents/algorithms/1st_thousand
  • src
    • main/java/com/fishercoder/solutions/firstthousand
    • test/java/com/fishercoder/firstthousand

3 files changed

+91
-0
lines changed

Diff for: paginated_contents/algorithms/1st_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@
397397
| 436 | [Find Right Interval](https://leetcode.com/problems/find-right-interval/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_436.java) | | Medium | Binary Search
398398
| 435 | [Non-overlapping Intervals](https://leetcode.com/problems/non-overlapping-intervals/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_435.java) | | Medium | Greedy
399399
| 434 | [Number of Segments in a String](https://leetcode.com/problems/number-of-segments-in-a-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_434.java) | | Easy |
400+
| 433 | [Minimum Genetic Mutation](https://leetcode.com/problems/minimum-genetic-mutation/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_433.java) | | Medium | BFS
400401
| 432 | [All O`one Data Structure](https://leetcode.com/problems/all-oone-data-structure/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_432.java) | | Hard | Design
401402
| 430 | [Flatten a Multilevel Doubly Linked List](https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_430.java) | | Medium | LinkedList, DFS, Doubly-Linked List
402403
| 429 | [N-ary Tree Level Order Traversal](https://leetcode.com/problems/n-ary-tree-level-order-traversal/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_429.java) | | Easy | BFS, Tree
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.fishercoder.solutions.firstthousand;
2+
3+
import java.util.HashSet;
4+
import java.util.LinkedList;
5+
import java.util.Queue;
6+
import java.util.Set;
7+
8+
public class _433 {
9+
public static class Solution1 {
10+
/**
11+
* My completely original solution, BFS.
12+
*/
13+
public int minMutation(String startGene, String endGene, String[] bank) {
14+
boolean found = false;
15+
for (String b : bank) {
16+
if (b.equals(endGene)) {
17+
found = true;
18+
}
19+
}
20+
if (!found) {
21+
return -1;
22+
}
23+
Queue<String> q = new LinkedList<>();
24+
q.offer(startGene);
25+
int mutations = 0;
26+
Set<String> used = new HashSet<>();
27+
used.add(startGene);
28+
while (!q.isEmpty()) {
29+
int size = q.size();
30+
for (int i = 0; i < size; i++) {
31+
String curr = q.poll();
32+
if (curr.equals(endGene)) {
33+
return mutations;
34+
}
35+
for (String candidate : bank) {
36+
if (oneDiff(curr, candidate) && used.add(candidate)) {
37+
q.offer(candidate);
38+
}
39+
}
40+
}
41+
mutations++;
42+
}
43+
return -1;
44+
}
45+
46+
private boolean oneDiff(String word1, String word2) {
47+
int diffChars = 0;
48+
for (int i = 0; i < word1.length(); i++) {
49+
if (word1.charAt(i) != word2.charAt(i)) {
50+
diffChars++;
51+
}
52+
}
53+
return diffChars == 1;
54+
}
55+
}
56+
}
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.fishercoder.firstthousand;
2+
3+
import com.fishercoder.solutions.firstthousand._433;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class _433Test {
10+
private static _433.Solution1 solution1;
11+
12+
@BeforeEach
13+
public void setup() {
14+
solution1 = new _433.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(-1, solution1.minMutation("AACCGGTT", "AACCGGTA", new String[]{}));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(-1, solution1.minMutation("AAAAAAAA", "CCCCCCCC",
25+
new String[]{"AAAAAAAA", "AAAAAAAC", "AAAAAACC", "AAAAACCC", "AAAACCCC", "AACACCCC", "ACCACCCC", "ACCCCCCC", "CCCCCCCA"}));
26+
}
27+
28+
@Test
29+
public void test3() {
30+
assertEquals(-1, solution1.minMutation("AAAAAAAT", "CCCCCCCC",
31+
new String[]{"AAAAAAAC", "AAAAAAAA", "CCCCCCCC"}));
32+
}
33+
34+
}

0 commit comments

Comments
 (0)