
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)254Please respect copyright.PENANALqZpqKjznD
// better than use DFS as it just need to find out the shortest path.
class Solution {254Please respect copyright.PENANAR7F1GhIJYc
public int minMutation(String start, String end, String[] bank) {254Please respect copyright.PENANAqkewlmxxS9
// 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.254Please respect copyright.PENANARgf8J9YHYH
Queue<String> queue = new LinkedList<>();254Please respect copyright.PENANAtkUgKSi4jR
Set<String> seen = new HashSet<>();254Please respect copyright.PENANAk9SEJjPkBx
queue.add(start);254Please respect copyright.PENANAySiaZm9UXg
seen.add(start);254Please respect copyright.PENANA7uaQvZ8jI0
254Please respect copyright.PENANAmcha6IWbMj
int steps = 0;254Please respect copyright.PENANA2n6oflfUeK
254Please respect copyright.PENANA1qmCEikAAa
while (!queue.isEmpty()) {254Please respect copyright.PENANAePQdpHsmlR
int nodesInQueue = queue.size();254Please respect copyright.PENANAsovIFzGicV
for (int j = 0; j < nodesInQueue; j++) {254Please respect copyright.PENANA7jWV5WHPN7
String node = queue.remove();254Please respect copyright.PENANAZTIhSfWCbe
// 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)) {254Please respect copyright.PENANAKHZkMlKsOr
return steps;254Please respect copyright.PENANAN2unqMZeeC
}
for (char c: new char[] {'A', 'C', 'G', 'T'}) {254Please respect copyright.PENANA2MH5VLdMK9
for (int i = 0; i < node.length(); i++) {254Please respect copyright.PENANAWNOcJ1zaIj
String neighbor = node.substring(0, i) + c + node.substring(i + 1);254Please respect copyright.PENANA7hv6FHdKSu
if (!seen.contains(neighbor) && Arrays.asList(bank).contains(neighbor)) {254Please respect copyright.PENANAthxSPHHXPK
queue.add(neighbor);254Please respect copyright.PENANAT8AGnz9HnG
seen.add(neighbor);254Please respect copyright.PENANAfwOUIbhxM9
}254Please respect copyright.PENANAjQhXhxY59c
}254Please respect copyright.PENANAOYiz03mmtj
}254Please respect copyright.PENANA9kEzUZPstY
}254Please respect copyright.PENANAa38kS48wCc
254Please respect copyright.PENANA8CrUYGZx1e
steps++;254Please respect copyright.PENANAjNQFEoxFsr
}254Please respect copyright.PENANAzqmQ1vCUqt
// If we finish the BFS and did not find end, return -1.254Please respect copyright.PENANA3j7CeAm6mk
return -1;254Please respect copyright.PENANA6zetWzm0LA
}254Please respect copyright.PENANApLdf4QL6Ei
}