
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)239Please respect copyright.PENANAHQSNu7vWH7
// better than use DFS as it just need to find out the shortest path.
class Solution {239Please respect copyright.PENANAOsyPGVnpxI
public int minMutation(String start, String end, String[] bank) {239Please respect copyright.PENANA4sLX7xSyJ7
// 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.239Please respect copyright.PENANAuTIssSDAjP
Queue<String> queue = new LinkedList<>();239Please respect copyright.PENANATFDQ45JgKx
Set<String> seen = new HashSet<>();239Please respect copyright.PENANA53WqiPoxMz
queue.add(start);239Please respect copyright.PENANAOXkFjRPhhi
seen.add(start);239Please respect copyright.PENANA3jEKnGrkTT
239Please respect copyright.PENANAa6B6tWmnJy
int steps = 0;239Please respect copyright.PENANA7HFuhp0OmW
239Please respect copyright.PENANAcLa16SWitT
while (!queue.isEmpty()) {239Please respect copyright.PENANAxBK8E3SZh5
int nodesInQueue = queue.size();239Please respect copyright.PENANAYXQKedd79i
for (int j = 0; j < nodesInQueue; j++) {239Please respect copyright.PENANACMQt9GkK3P
String node = queue.remove();239Please respect copyright.PENANAGft6ymoxec
// 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)) {239Please respect copyright.PENANAJzqZPfcyYc
return steps;239Please respect copyright.PENANA8xFaXbzdPz
}
for (char c: new char[] {'A', 'C', 'G', 'T'}) {239Please respect copyright.PENANAzy7sLxXpu6
for (int i = 0; i < node.length(); i++) {239Please respect copyright.PENANAmKkza02KsD
String neighbor = node.substring(0, i) + c + node.substring(i + 1);239Please respect copyright.PENANAHbcZByXYna
if (!seen.contains(neighbor) && Arrays.asList(bank).contains(neighbor)) {239Please respect copyright.PENANALGHyCCBqS6
queue.add(neighbor);239Please respect copyright.PENANAybAieP96fp
seen.add(neighbor);239Please respect copyright.PENANAjdLPXIjfrI
}239Please respect copyright.PENANAc5Kk1Rz0pU
}239Please respect copyright.PENANAMQ0WJLBqem
}239Please respect copyright.PENANAT35AGcPV75
}239Please respect copyright.PENANA2VPht6gKSY
239Please respect copyright.PENANAuTyI1dLha3
steps++;239Please respect copyright.PENANAyBMZ8VPhj0
}239Please respect copyright.PENANAldoeTy7S3d
// If we finish the BFS and did not find end, return -1.239Please respect copyright.PENANAKvN1UIQpNg
return -1;239Please respect copyright.PENANAlyXoObM25z
}239Please respect copyright.PENANAWZmDMsgdA0
}