
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)267Please respect copyright.PENANArK1I90yUxM
// better than use DFS as it just need to find out the shortest path.
class Solution {267Please respect copyright.PENANADRO9zslkIv
public int minMutation(String start, String end, String[] bank) {267Please respect copyright.PENANA0bsd05PccD
// 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.267Please respect copyright.PENANAKq5anqfLSf
Queue<String> queue = new LinkedList<>();267Please respect copyright.PENANAnY1So2Hiwc
Set<String> seen = new HashSet<>();267Please respect copyright.PENANAimTnUIGir8
queue.add(start);267Please respect copyright.PENANAArHM765AxM
seen.add(start);267Please respect copyright.PENANAbcQlgqiVz6
267Please respect copyright.PENANARePq4Z98Mq
int steps = 0;267Please respect copyright.PENANAsyK9AYNP7H
267Please respect copyright.PENANAu1ujVwv69h
while (!queue.isEmpty()) {267Please respect copyright.PENANAkrIn1Xlz58
int nodesInQueue = queue.size();267Please respect copyright.PENANAQd6iHX7OYT
for (int j = 0; j < nodesInQueue; j++) {267Please respect copyright.PENANAUrdWqChIw8
String node = queue.remove();267Please respect copyright.PENANAvi8CJld9c2
// 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)) {267Please respect copyright.PENANAB8qc0FrA7m
return steps;267Please respect copyright.PENANAVSIFvM8KAL
}
for (char c: new char[] {'A', 'C', 'G', 'T'}) {267Please respect copyright.PENANAfIkSRX3Nta
for (int i = 0; i < node.length(); i++) {267Please respect copyright.PENANAotshuRKvP2
String neighbor = node.substring(0, i) + c + node.substring(i + 1);267Please respect copyright.PENANAA5DGhRRMwt
if (!seen.contains(neighbor) && Arrays.asList(bank).contains(neighbor)) {267Please respect copyright.PENANAJn5fPFXAyc
queue.add(neighbor);267Please respect copyright.PENANAxDMwDxV90d
seen.add(neighbor);267Please respect copyright.PENANAUCoWmFYGJO
}267Please respect copyright.PENANA3RX6R9o4H2
}267Please respect copyright.PENANAPauePPMI5l
}267Please respect copyright.PENANAOdaCjNcFDQ
}267Please respect copyright.PENANAJsbf6W4rMq
267Please respect copyright.PENANARGWovzxthf
steps++;267Please respect copyright.PENANA2tUQ5nq8vy
}267Please respect copyright.PENANA5kGKVgcMVV
// If we finish the BFS and did not find end, return -1.267Please respect copyright.PENANAgkB5gijWWl
return -1;267Please respect copyright.PENANAMgZmFilsZo
}267Please respect copyright.PENANA7R5eGoAQRn
}