-
Notifications
You must be signed in to change notification settings - Fork 4
/
MinimumGeneticMutation.java
32 lines (29 loc) · 1.19 KB
/
MinimumGeneticMutation.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
package com.dbc;
import javafx.util.Pair;
import java.util.*;
public class MinimumGeneticMutation {
public int minMutation(String start, String end, String[] bank) {
if (start.equals(end)) return 0;
Set<String> set = new HashSet<>(Arrays.asList(bank));
if (!set.contains(end)) return -1;
char[] tran = new char[]{'A', 'C', 'G', 'T'};
Deque<Pair<String, Integer>> queue = new ArrayDeque<>();
queue.offerFirst(new Pair<>(start, 0));
while (!queue.isEmpty()) {
Pair<String, Integer> pair = queue.pollFirst();
for (int i = 0; i < pair.getKey().length(); i++) {
for (char ch : tran) {
if (ch != pair.getKey().charAt(i)) {
String temp = pair.getKey().substring(0, i) + ch + pair.getKey().substring(i + 1);
if (set.contains(temp)) {
if (temp.equals(end)) return pair.getValue() + 1;
set.remove(temp);
queue.addLast(new Pair<>(temp, pair.getValue() + 1));
}
}
}
}
}
return -1;
}
}