
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)210Please respect copyright.PENANA0PdPPMFozg
// better than use DFS as it just need to find out the shortest path.
class Solution {210Please respect copyright.PENANAol5ds3KcUO
public int minMutation(String start, String end, String[] bank) {210Please respect copyright.PENANAkbYK0u42H0
// 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.210Please respect copyright.PENANAtpk60x05o4
Queue<String> queue = new LinkedList<>();210Please respect copyright.PENANAu12risv3MV
Set<String> seen = new HashSet<>();210Please respect copyright.PENANAwFkKsSJspA
queue.add(start);210Please respect copyright.PENANAsHp23eopeh
seen.add(start);210Please respect copyright.PENANApbDkPtA9HV
210Please respect copyright.PENANAINZ1D4wFbM
int steps = 0;210Please respect copyright.PENANAtiVJ6qY7p9
210Please respect copyright.PENANADJnSjbCBfw
while (!queue.isEmpty()) {210Please respect copyright.PENANAuS20NC2aIr
int nodesInQueue = queue.size();210Please respect copyright.PENANAt8NnaGa3Xf
for (int j = 0; j < nodesInQueue; j++) {210Please respect copyright.PENANAHiIBSLhbcV
String node = queue.remove();210Please respect copyright.PENANAK20fyA0fyU
// 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)) {210Please respect copyright.PENANApkny4yMIoX
return steps;210Please respect copyright.PENANAT6LZuTNIZH
}
for (char c: new char[] {'A', 'C', 'G', 'T'}) {210Please respect copyright.PENANABpnKTaBEjK
for (int i = 0; i < node.length(); i++) {210Please respect copyright.PENANAoPlk7HIImc
String neighbor = node.substring(0, i) + c + node.substring(i + 1);210Please respect copyright.PENANABFJ8xF6BAK
if (!seen.contains(neighbor) && Arrays.asList(bank).contains(neighbor)) {210Please respect copyright.PENANAey4IBnQ9RX
queue.add(neighbor);210Please respect copyright.PENANA1KTlfavIRn
seen.add(neighbor);210Please respect copyright.PENANACnshdVWqUf
}210Please respect copyright.PENANAaLkC7aOrRX
}210Please respect copyright.PENANAHxw7iu5ynE
}210Please respect copyright.PENANAQjXqhBDqwR
}210Please respect copyright.PENANAeFsr5BxsBl
210Please respect copyright.PENANAFOI5sMtod8
steps++;210Please respect copyright.PENANACigd8Nmdmy
}210Please respect copyright.PENANA1RY1YXonV6
// If we finish the BFS and did not find end, return -1.210Please respect copyright.PENANAnoZE4ssQib
return -1;210Please respect copyright.PENANAPWQDzItCw7
}210Please respect copyright.PENANAONqyovaJCD
}