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)426Please respect copyright.PENANALA2DTiQeaM
// better than use DFS as it just need to find out the shortest path.
class Solution {426Please respect copyright.PENANAoSLpGOPJx5
public int minMutation(String start, String end, String[] bank) {426Please respect copyright.PENANAMsZ80SqWlP
// 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.426Please respect copyright.PENANAHt66dGtBSZ
Queue<String> queue = new LinkedList<>();426Please respect copyright.PENANAeDoiCAUxGM
Set<String> seen = new HashSet<>();426Please respect copyright.PENANAYEY4iSJ9e0
queue.add(start);426Please respect copyright.PENANABKWnNA8CRv
seen.add(start);426Please respect copyright.PENANAKsDtVJSGzR
426Please respect copyright.PENANAhLkoLDc4ZC
int steps = 0;426Please respect copyright.PENANAImhqRIGZkr
426Please respect copyright.PENANAidtYhnK1if
while (!queue.isEmpty()) {426Please respect copyright.PENANAl2wogrWv6s
int nodesInQueue = queue.size();426Please respect copyright.PENANASzv3n39S9m
for (int j = 0; j < nodesInQueue; j++) {426Please respect copyright.PENANAa6x76nNXQU
String node = queue.remove();426Please respect copyright.PENANAVcKjvFvcH7
// 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)) {426Please respect copyright.PENANAOdd8oBdmTV
return steps;426Please respect copyright.PENANAluPSruqewN
}
for (char c: new char[] {'A', 'C', 'G', 'T'}) {426Please respect copyright.PENANAjQBpBs47Ie
for (int i = 0; i < node.length(); i++) {426Please respect copyright.PENANAqloqYL7xc3
String neighbor = node.substring(0, i) + c + node.substring(i + 1);426Please respect copyright.PENANAoXT2dGsvvz
if (!seen.contains(neighbor) && Arrays.asList(bank).contains(neighbor)) {426Please respect copyright.PENANAAbSTYsQ7yT
queue.add(neighbor);426Please respect copyright.PENANAkElDs5HYMK
seen.add(neighbor);426Please respect copyright.PENANAymaQAhNI49
}426Please respect copyright.PENANASAbukhu5ln
}426Please respect copyright.PENANAsdYWuoprmj
}426Please respect copyright.PENANAh6eKrelNbC
}426Please respect copyright.PENANAdMsLzAbZn0
426Please respect copyright.PENANA4yh9WAN7cp
steps++;426Please respect copyright.PENANAheSm9p10Xo
}426Please respect copyright.PENANA9vFsWMqIbY
// If we finish the BFS and did not find end, return -1.426Please respect copyright.PENANAesVwO6UPYq
return -1;426Please respect copyright.PENANAIQIt7Mpu5y
}426Please respect copyright.PENANANVh0ORIXcL
}


