
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)195Please respect copyright.PENANAm4QBb4l3Nd
// better than use DFS as it just need to find out the shortest path.
class Solution {195Please respect copyright.PENANApSWoXNNBl7
public int minMutation(String start, String end, String[] bank) {195Please respect copyright.PENANAueufbIvvmd
// 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.195Please respect copyright.PENANA8fvNL4FnZD
Queue<String> queue = new LinkedList<>();195Please respect copyright.PENANAvDiJyisn06
Set<String> seen = new HashSet<>();195Please respect copyright.PENANAS0Ni6ykonV
queue.add(start);195Please respect copyright.PENANAHnepYPbwgK
seen.add(start);195Please respect copyright.PENANAZfBrm3VqNA
195Please respect copyright.PENANABZ52k4ppd6
int steps = 0;195Please respect copyright.PENANAuHMUR2cQtt
195Please respect copyright.PENANAaI3CFjOJi5
while (!queue.isEmpty()) {195Please respect copyright.PENANArLvPVMBeeK
int nodesInQueue = queue.size();195Please respect copyright.PENANA3lVwiMKPbH
for (int j = 0; j < nodesInQueue; j++) {195Please respect copyright.PENANA4AHhvrSBP1
String node = queue.remove();195Please respect copyright.PENANAny3W6UvgBe
// 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)) {195Please respect copyright.PENANA0UILYDVeV1
return steps;195Please respect copyright.PENANA3JQAuB0wrY
}
for (char c: new char[] {'A', 'C', 'G', 'T'}) {195Please respect copyright.PENANAYbyFnRGsnQ
for (int i = 0; i < node.length(); i++) {195Please respect copyright.PENANAh6TAoWqiKZ
String neighbor = node.substring(0, i) + c + node.substring(i + 1);195Please respect copyright.PENANAmXRLtwGUWi
if (!seen.contains(neighbor) && Arrays.asList(bank).contains(neighbor)) {195Please respect copyright.PENANAjCJK2XdpFg
queue.add(neighbor);195Please respect copyright.PENANA2SA3Q10nwB
seen.add(neighbor);195Please respect copyright.PENANA5fDX0TQcEf
}195Please respect copyright.PENANAsAfwBWSvpH
}195Please respect copyright.PENANABOfR8tAW4M
}195Please respect copyright.PENANADKEYX29B6k
}195Please respect copyright.PENANACxkLe1Nwci
195Please respect copyright.PENANA5IQfAWUjTj
steps++;195Please respect copyright.PENANAjQzqseIfpz
}195Please respect copyright.PENANA9NlaakcJhK
// If we finish the BFS and did not find end, return -1.195Please respect copyright.PENANAAG8Rz2Vf4G
return -1;195Please respect copyright.PENANAyBscPsACRd
}195Please respect copyright.PENANAyaBqP997ch
}