
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)233Please respect copyright.PENANAynD2fKbVaH
// better than use DFS as it just need to find out the shortest path.
class Solution {233Please respect copyright.PENANALSayhQ9e8S
public int minMutation(String start, String end, String[] bank) {233Please respect copyright.PENANACqDJFbOodi
// 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.233Please respect copyright.PENANA7xZ5GeYMfx
Queue<String> queue = new LinkedList<>();233Please respect copyright.PENANApcozrznrdX
Set<String> seen = new HashSet<>();233Please respect copyright.PENANATRue4ajbKQ
queue.add(start);233Please respect copyright.PENANAUvs3teSGmM
seen.add(start);233Please respect copyright.PENANARcFl5QFrVt
233Please respect copyright.PENANAAerosCYC6A
int steps = 0;233Please respect copyright.PENANARDwtkuUdIK
233Please respect copyright.PENANASxg84MQq50
while (!queue.isEmpty()) {233Please respect copyright.PENANAGRZ2vJiPNV
int nodesInQueue = queue.size();233Please respect copyright.PENANAAiqLa0ejD2
for (int j = 0; j < nodesInQueue; j++) {233Please respect copyright.PENANAmF3A1nIJ85
String node = queue.remove();233Please respect copyright.PENANArufcoUBTLI
// 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)) {233Please respect copyright.PENANAtOaS8Nzehy
return steps;233Please respect copyright.PENANA7F0Gicv2Zt
}
for (char c: new char[] {'A', 'C', 'G', 'T'}) {233Please respect copyright.PENANA6AytlFBLJS
for (int i = 0; i < node.length(); i++) {233Please respect copyright.PENANAkB4TyEU8jO
String neighbor = node.substring(0, i) + c + node.substring(i + 1);233Please respect copyright.PENANAVkq32stb44
if (!seen.contains(neighbor) && Arrays.asList(bank).contains(neighbor)) {233Please respect copyright.PENANAaylHnuZUVF
queue.add(neighbor);233Please respect copyright.PENANARlZOlSc0qw
seen.add(neighbor);233Please respect copyright.PENANA4q5gYLw8xM
}233Please respect copyright.PENANACzKhkFmMKK
}233Please respect copyright.PENANA4F9RmkWccH
}233Please respect copyright.PENANAHNFw4cZRet
}233Please respect copyright.PENANAUWlBkOusIh
233Please respect copyright.PENANAH8McH2zSao
steps++;233Please respect copyright.PENANAYKQsC7jSzK
}233Please respect copyright.PENANAqrfzsY7uOR
// If we finish the BFS and did not find end, return -1.233Please respect copyright.PENANAvisQuofyPc
return -1;233Please respect copyright.PENANANAISuvu526
}233Please respect copyright.PENANAEBasaOSY5o
}