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)114Please respect copyright.PENANADWEmG9Fdcl
// better than use DFS as it just need to find out the shortest path.
class Solution {114Please respect copyright.PENANAYziGolkQ4E
public int minMutation(String start, String end, String[] bank) {114Please respect copyright.PENANAGM2lqjaGj9
// 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.114Please respect copyright.PENANAt9N50uFmxR
Queue<String> queue = new LinkedList<>();114Please respect copyright.PENANAeZYVTMri2p
Set<String> seen = new HashSet<>();114Please respect copyright.PENANAUMk8ZbSBGT
queue.add(start);114Please respect copyright.PENANA4k4iuHL7av
seen.add(start);114Please respect copyright.PENANArjgkh5D2Q9
114Please respect copyright.PENANAz9r4KBJSRu
int steps = 0;114Please respect copyright.PENANAsSkELcvMkb
114Please respect copyright.PENANA3cLn8konfT
while (!queue.isEmpty()) {114Please respect copyright.PENANAvML2AHicE7
int nodesInQueue = queue.size();114Please respect copyright.PENANAtVawB0AJXs
for (int j = 0; j < nodesInQueue; j++) {114Please respect copyright.PENANAermdGzQIwv
String node = queue.remove();114Please respect copyright.PENANAEvxrI8s3hJ
// 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)) {114Please respect copyright.PENANAQbhCCKG170
return steps;114Please respect copyright.PENANAcZ3PYLgLHM
}
for (char c: new char[] {'A', 'C', 'G', 'T'}) {114Please respect copyright.PENANARdD5xnhopJ
for (int i = 0; i < node.length(); i++) {114Please respect copyright.PENANAPEtjP9zyQS
String neighbor = node.substring(0, i) + c + node.substring(i + 1);114Please respect copyright.PENANAQcGn3RY3sk
if (!seen.contains(neighbor) && Arrays.asList(bank).contains(neighbor)) {114Please respect copyright.PENANA3IIK7Px2yl
queue.add(neighbor);114Please respect copyright.PENANAYJZzUZ5sRf
seen.add(neighbor);114Please respect copyright.PENANAGiJ10HgqN8
}114Please respect copyright.PENANA6KwIkj9XUy
}114Please respect copyright.PENANApbHntejetE
}114Please respect copyright.PENANAU83v6Po7My
}114Please respect copyright.PENANAoOTxZJkiCf
114Please respect copyright.PENANAmsJrfPjpun
steps++;114Please respect copyright.PENANAwN2F5sVQb8
}114Please respect copyright.PENANAQkjSu4irLT
// If we finish the BFS and did not find end, return -1.114Please respect copyright.PENANAcpvSCAs9ty
return -1;114Please respect copyright.PENANAqnOyBpZhYE
}114Please respect copyright.PENANAWyhwhfx3T2
}