-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution433.java
More file actions
56 lines (48 loc) · 1.64 KB
/
Solution433.java
File metadata and controls
56 lines (48 loc) · 1.64 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
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;
public class Solution433 {
public static int minMutation(String start, String end, String[] bank) {
if (bank == null || bank.length == 0 || start.equals(end)) {
return -1;
}
Set<String> bankSet = new HashSet<String>();
for (int i = 0; i < bank.length; i++) {
bankSet.add(bank[i]);
}
Queue<String> queue = new LinkedList<String>();
char[] geneChar = {'A', 'C', 'G', 'T'};
int count = 0;
queue.offer(start);
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
String curr = queue.poll();
if (curr.equals(end)) {
return count;
}
for (int j = 0; j < curr.length(); j++) {
StringBuilder sb = new StringBuilder(curr);
for (char c : geneChar) {
sb.setCharAt(j, c);
String word = sb.toString();
if (bankSet.contains(word)) {
bankSet.remove(word);
queue.offer(word);
}
}
}
}
count++;
}
return -1;
}
public static void main(String[] args) {
String start = "AACCGGTT";
String end = "AACCGGTA";
String[] bank = {"AACCGGTA"};
int count = minMutation(start, end, bank);
System.out.println(count);
}
}