Q: A gene string can be represented by an 8-character long string, with choices from 'A', 'C', 'G', and 'T'.
Suppose we need to investigate a mutation from a gene string start to a gene string end where one mutation is defined as one single character changed in the gene string.
- For example, "AACCGGTT" --> "AACCGGTA" is one mutation.
There is also a gene bank bank that records all the valid gene mutations. A gene must be in bank to make it a valid gene string.
Given the two gene strings start and end and the gene bank bank, return the minimum number of mutations needed to mutate from start to end. If there is no such a mutation, return -1.
Note that the starting point is assumed to be valid, so it might not be included in the bank.
A: BFS (Breadth-First Search)140Please respect copyright.PENANABnFJ3OyJmh
// better than use DFS as it just need to find out the shortest path.
class Solution {140Please respect copyright.PENANAAytyNBy4Jr
public int minMutation(String start, String end, String[] bank) {140Please respect copyright.PENANAb8HNN31n40
// Initialize a queue queue and a set seen. The queue will be used for BFS and the set will be used to prevent visiting a node more than once. Initially, the queue and set should hold start.140Please respect copyright.PENANAwSMeTLXoBg
Queue<String> queue = new LinkedList<>();140Please respect copyright.PENANA2QOxMalUxf
Set<String> seen = new HashSet<>();140Please respect copyright.PENANAEJXMGpzc9f
queue.add(start);140Please respect copyright.PENANAe7URf2rDgG
seen.add(start);140Please respect copyright.PENANAKSkdNGLEcX
140Please respect copyright.PENANAaoyKksmmuL
int steps = 0;140Please respect copyright.PENANAEkefnyZxeE
140Please respect copyright.PENANAUoVvGjPixv
while (!queue.isEmpty()) {140Please respect copyright.PENANAv62IRxtFdh
int nodesInQueue = queue.size();140Please respect copyright.PENANArs4tGSpvnY
for (int j = 0; j < nodesInQueue; j++) {140Please respect copyright.PENANAvVxlPbk3KI
String node = queue.remove();140Please respect copyright.PENANAlEP4PQczWh
// Perform a BFS. At each node, if node == end, return the number of steps so far. Otherwise, iterate over all the neighbors. For each neighbor, if neighbor is not in seen and neighbor is in bank, add it to queue and seen.
if (node.equals(end)) {140Please respect copyright.PENANAFhT88UOXui
return steps;140Please respect copyright.PENANA7S2lmvVgQt
}
for (char c: new char[] {'A', 'C', 'G', 'T'}) {140Please respect copyright.PENANAm9aDD0iDkp
for (int i = 0; i < node.length(); i++) {140Please respect copyright.PENANAg9pVXntz7c
String neighbor = node.substring(0, i) + c + node.substring(i + 1);140Please respect copyright.PENANAwmHQHJUcwq
if (!seen.contains(neighbor) && Arrays.asList(bank).contains(neighbor)) {140Please respect copyright.PENANAhLdIgIXS58
queue.add(neighbor);140Please respect copyright.PENANA5rrSIYFnBN
seen.add(neighbor);140Please respect copyright.PENANACzjV6B97GD
}140Please respect copyright.PENANAr8N7daXNW8
}140Please respect copyright.PENANA55OkpcjDGK
}140Please respect copyright.PENANAJnUiq2JQnt
}140Please respect copyright.PENANAkb1501nvHZ
140Please respect copyright.PENANAPX03kmcSKp
steps++;140Please respect copyright.PENANA9dIgCgImkD
}140Please respect copyright.PENANAqXQibbaKHg
// If we finish the BFS and did not find end, return -1.140Please respect copyright.PENANADAjofsDBqy
return -1;140Please respect copyright.PENANAo1VItE7PTN
}140Please respect copyright.PENANA4D4XLfabb6
}