
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)212Please respect copyright.PENANA9BVznwknWW
// better than use DFS as it just need to find out the shortest path.
class Solution {212Please respect copyright.PENANA7Qz4HOehAj
public int minMutation(String start, String end, String[] bank) {212Please respect copyright.PENANAGsRNsCNm7f
// 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.212Please respect copyright.PENANAJRWekhCTLl
Queue<String> queue = new LinkedList<>();212Please respect copyright.PENANA4NT1zB7Elj
Set<String> seen = new HashSet<>();212Please respect copyright.PENANAcP5rHWO5y0
queue.add(start);212Please respect copyright.PENANAQaCdiINs3b
seen.add(start);212Please respect copyright.PENANA9n0kbgg8o7
212Please respect copyright.PENANA7ppfy07UJM
int steps = 0;212Please respect copyright.PENANA6F6T8Iljyj
212Please respect copyright.PENANAaDCfdAmZ45
while (!queue.isEmpty()) {212Please respect copyright.PENANAVRigwnP0ck
int nodesInQueue = queue.size();212Please respect copyright.PENANAV4FdyOdhYU
for (int j = 0; j < nodesInQueue; j++) {212Please respect copyright.PENANAj7iblD7PXh
String node = queue.remove();212Please respect copyright.PENANA8IG9UHrvOb
// 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)) {212Please respect copyright.PENANA6jMJTnD8JJ
return steps;212Please respect copyright.PENANAK0C3vhksNX
}
for (char c: new char[] {'A', 'C', 'G', 'T'}) {212Please respect copyright.PENANAYX4gVuiJZ1
for (int i = 0; i < node.length(); i++) {212Please respect copyright.PENANAxmDZ7oxzwl
String neighbor = node.substring(0, i) + c + node.substring(i + 1);212Please respect copyright.PENANAuTyOSzdPIS
if (!seen.contains(neighbor) && Arrays.asList(bank).contains(neighbor)) {212Please respect copyright.PENANASO7DN5L7Gi
queue.add(neighbor);212Please respect copyright.PENANAZALkTvZS3l
seen.add(neighbor);212Please respect copyright.PENANAtJ2QpDljXP
}212Please respect copyright.PENANAO5ZdjgCol9
}212Please respect copyright.PENANAeIWSP4GOXT
}212Please respect copyright.PENANA1BwW3VHzBr
}212Please respect copyright.PENANA39xJNFvS0C
212Please respect copyright.PENANAyWkSr83Dyt
steps++;212Please respect copyright.PENANA6t1LDoyqW8
}212Please respect copyright.PENANAcK122YHL7Q
// If we finish the BFS and did not find end, return -1.212Please respect copyright.PENANAHwiykVn6Uu
return -1;212Please respect copyright.PENANAuIH9KR6ejK
}212Please respect copyright.PENANAgB8ep5HDda
}