-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path433. Minimum Genetic Mutation.java
38 lines (38 loc) · 1.63 KB
/
433. Minimum Genetic Mutation.java
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
class Solution {
public int minMutation(String start, String end, String[] bank) {
int answer = 0;
Set<String> bankset = new HashSet<>(Arrays.stream(bank).toList());
Set<String> computed = new HashSet<>();
Queue<String> queue = new ConcurrentLinkedQueue<>();
char[] options = new char[] { 'A', 'C', 'G', 'T' };
queue.offer(start);
computed.add(start);
while (!queue.isEmpty()) {
int size = queue.size();
while (size > 0) {
String current = queue.poll();
if (current.equals(end))
return answer;
for (char opt : options) {
// all characters index in current
for (int i = 0; i < current.length(); i++) {
StringBuilder neighbour = new StringBuilder(current);
neighbour.setCharAt(i, opt);
// is this new modified neighnboir in bankset or visited
if (!computed.contains(neighbour.toString()) && bankset.contains(neighbour.toString())) {
queue.offer(neighbour.toString());
computed.add(neighbour.toString());
}
}
}
size--;
}
answer++;
}
return -1;
}
}