-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.java
33 lines (33 loc) · 1.09 KB
/
Solution.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
class Solution {
public int minMutation(String start, String end, String[] bank) {
Set<String> s = new HashSet<>();
for (String b : bank) {
s.add(b);
}
Map<Character, String> mp = new HashMap<>(4);
mp.put('A', "TCG");
mp.put('T', "ACG");
mp.put('C', "ATG");
mp.put('G', "ATC");
Deque<Pair<String, Integer>> q = new LinkedList<>();
q.offer(new Pair<>(start, 0));
while (!q.isEmpty()) {
Pair<String, Integer> p = q.poll();
String t = p.getKey();
int step = p.getValue();
if (end.equals(t)) {
return step;
}
for (int i = 0; i < t.length(); ++i) {
for (char c : mp.get(t.charAt(i)).toCharArray()) {
String next = t.substring(0, i) + c + t.substring(i + 1);
if (s.contains(next)) {
q.offer(new Pair<>(next, step + 1));
s.remove(next);
}
}
}
}
return -1;
}
}