
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)293Please respect copyright.PENANAgzNADFRaQl
// better than use DFS as it just need to find out the shortest path.
class Solution {293Please respect copyright.PENANAuwf2u7dUAO
public int minMutation(String start, String end, String[] bank) {293Please respect copyright.PENANACLfKDi0H1L
// 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.293Please respect copyright.PENANAQiQiuBjUz7
Queue<String> queue = new LinkedList<>();293Please respect copyright.PENANA8pr6tO6Clp
Set<String> seen = new HashSet<>();293Please respect copyright.PENANAB20PThYDQL
queue.add(start);293Please respect copyright.PENANAvmpHU7p448
seen.add(start);293Please respect copyright.PENANANeRQs4Rb4g
293Please respect copyright.PENANARM2quQTRp5
int steps = 0;293Please respect copyright.PENANANiPXl4wbGM
293Please respect copyright.PENANAUdkFyyAMWp
while (!queue.isEmpty()) {293Please respect copyright.PENANAICkNiI0hWU
int nodesInQueue = queue.size();293Please respect copyright.PENANAzVN5DXMAcV
for (int j = 0; j < nodesInQueue; j++) {293Please respect copyright.PENANAvEXX0G9jer
String node = queue.remove();293Please respect copyright.PENANAkVQopxotQ9
// 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)) {293Please respect copyright.PENANAH0i3dd55am
return steps;293Please respect copyright.PENANAlegjqOPbHA
}
for (char c: new char[] {'A', 'C', 'G', 'T'}) {293Please respect copyright.PENANAqE1SdtIQVp
for (int i = 0; i < node.length(); i++) {293Please respect copyright.PENANAA5piYZdoJs
String neighbor = node.substring(0, i) + c + node.substring(i + 1);293Please respect copyright.PENANADH1uUMkKgF
if (!seen.contains(neighbor) && Arrays.asList(bank).contains(neighbor)) {293Please respect copyright.PENANAC7Yx9rw0Hj
queue.add(neighbor);293Please respect copyright.PENANAbgydH7WN7z
seen.add(neighbor);293Please respect copyright.PENANAOnmlimSc3V
}293Please respect copyright.PENANA9F52UOJuEs
}293Please respect copyright.PENANAOYepg1sT45
}293Please respect copyright.PENANAyYShUD3PLw
}293Please respect copyright.PENANA0SXEQsG9iJ
293Please respect copyright.PENANA8DjfiOd2fY
steps++;293Please respect copyright.PENANA8KlnO9UWwc
}293Please respect copyright.PENANAHDivQTX559
// If we finish the BFS and did not find end, return -1.293Please respect copyright.PENANAmT7k25guOz
return -1;293Please respect copyright.PENANAh9H1sghUS7
}293Please respect copyright.PENANADofWa2PloG
}