
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)217Please respect copyright.PENANAfFhPT4qi3V
// better than use DFS as it just need to find out the shortest path.
class Solution {217Please respect copyright.PENANAnpu1VZM4QI
public int minMutation(String start, String end, String[] bank) {217Please respect copyright.PENANAOGLa4oOJxM
// 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.217Please respect copyright.PENANAAeIVFawi5H
Queue<String> queue = new LinkedList<>();217Please respect copyright.PENANA0WcrvjKecx
Set<String> seen = new HashSet<>();217Please respect copyright.PENANAE9v3ihQLnv
queue.add(start);217Please respect copyright.PENANAZ7sVS55ePF
seen.add(start);217Please respect copyright.PENANAQDRvO9gznj
217Please respect copyright.PENANAH815SdZv0o
int steps = 0;217Please respect copyright.PENANA5r49guEt4K
217Please respect copyright.PENANAUq8Wd1f0lb
while (!queue.isEmpty()) {217Please respect copyright.PENANAMnfAYh6mmQ
int nodesInQueue = queue.size();217Please respect copyright.PENANAfQloSJ0asU
for (int j = 0; j < nodesInQueue; j++) {217Please respect copyright.PENANAw17ns9ryYb
String node = queue.remove();217Please respect copyright.PENANA4tLAOJmQ1M
// 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)) {217Please respect copyright.PENANAU2vnEuBszF
return steps;217Please respect copyright.PENANAsbzmErGLMV
}
for (char c: new char[] {'A', 'C', 'G', 'T'}) {217Please respect copyright.PENANASrpCBU71Y4
for (int i = 0; i < node.length(); i++) {217Please respect copyright.PENANAT1yfJJyGyU
String neighbor = node.substring(0, i) + c + node.substring(i + 1);217Please respect copyright.PENANAx1MF3lKdWS
if (!seen.contains(neighbor) && Arrays.asList(bank).contains(neighbor)) {217Please respect copyright.PENANArFlL0Tbzzq
queue.add(neighbor);217Please respect copyright.PENANA9HVeCkmAhx
seen.add(neighbor);217Please respect copyright.PENANABw2Z86P6bX
}217Please respect copyright.PENANAPnY9SzcS6D
}217Please respect copyright.PENANAs0iCQlGVJG
}217Please respect copyright.PENANAIfr84ArlsX
}217Please respect copyright.PENANAiwCFH1fdka
217Please respect copyright.PENANAVHutmJbRDw
steps++;217Please respect copyright.PENANAxMkRX5YZ3D
}217Please respect copyright.PENANAXTtrsYvhdO
// If we finish the BFS and did not find end, return -1.217Please respect copyright.PENANAtY4ekdaROb
return -1;217Please respect copyright.PENANA2s36GUf63n
}217Please respect copyright.PENANAYyctKLlI0s
}