
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)254Please respect copyright.PENANAbXEFqnHLC1
// better than use DFS as it just need to find out the shortest path.
class Solution {254Please respect copyright.PENANAWl2oRkPym3
public int minMutation(String start, String end, String[] bank) {254Please respect copyright.PENANAg3s2HFbqHn
// 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.254Please respect copyright.PENANAcyf2uCMqeS
Queue<String> queue = new LinkedList<>();254Please respect copyright.PENANA84r0Wdgd2p
Set<String> seen = new HashSet<>();254Please respect copyright.PENANA2gxWLHMfno
queue.add(start);254Please respect copyright.PENANAZxnFE008EP
seen.add(start);254Please respect copyright.PENANAdi8hnJNSho
254Please respect copyright.PENANAHNNILdqbLq
int steps = 0;254Please respect copyright.PENANAC6KXNmqc2W
254Please respect copyright.PENANAjpScn4Mlpy
while (!queue.isEmpty()) {254Please respect copyright.PENANA0556XeRpal
int nodesInQueue = queue.size();254Please respect copyright.PENANARzgCQrsAkv
for (int j = 0; j < nodesInQueue; j++) {254Please respect copyright.PENANA043KXJfYhi
String node = queue.remove();254Please respect copyright.PENANAgAT9U2X8rP
// 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)) {254Please respect copyright.PENANAOsFoJjoXtl
return steps;254Please respect copyright.PENANA8KO3K7eJp7
}
for (char c: new char[] {'A', 'C', 'G', 'T'}) {254Please respect copyright.PENANAdQ1jFAMaHa
for (int i = 0; i < node.length(); i++) {254Please respect copyright.PENANAiD94rIggEp
String neighbor = node.substring(0, i) + c + node.substring(i + 1);254Please respect copyright.PENANA9SV7WRfils
if (!seen.contains(neighbor) && Arrays.asList(bank).contains(neighbor)) {254Please respect copyright.PENANAho7AtQ36Rt
queue.add(neighbor);254Please respect copyright.PENANAxm6CIFclYn
seen.add(neighbor);254Please respect copyright.PENANA3VhLbhc30e
}254Please respect copyright.PENANAvEiQAmiXTB
}254Please respect copyright.PENANArKd7xukHr5
}254Please respect copyright.PENANAkyo734lXDW
}254Please respect copyright.PENANALXP4lSxVa3
254Please respect copyright.PENANAzVYJpMcO0P
steps++;254Please respect copyright.PENANA9pxeisJGFo
}254Please respect copyright.PENANASdSBFAsaPQ
// If we finish the BFS and did not find end, return -1.254Please respect copyright.PENANA83bqmiz8s0
return -1;254Please respect copyright.PENANAbIoSEneqWI
}254Please respect copyright.PENANAcOlAFycwgY
}