
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)234Please respect copyright.PENANA5VMcvRxXJy
// better than use DFS as it just need to find out the shortest path.
class Solution {234Please respect copyright.PENANAay2cwhE7uv
public int minMutation(String start, String end, String[] bank) {234Please respect copyright.PENANAJmldHPMfyv
// 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.234Please respect copyright.PENANAye7qOTLsPj
Queue<String> queue = new LinkedList<>();234Please respect copyright.PENANAITRXbLQXGZ
Set<String> seen = new HashSet<>();234Please respect copyright.PENANAcWf9PvahFs
queue.add(start);234Please respect copyright.PENANAFHEMwcFwFO
seen.add(start);234Please respect copyright.PENANAn9kbA12Hfi
234Please respect copyright.PENANAlVoRWNuwrU
int steps = 0;234Please respect copyright.PENANAOVHwydLBMw
234Please respect copyright.PENANAMIsIafQwFO
while (!queue.isEmpty()) {234Please respect copyright.PENANAszFtCQAWtJ
int nodesInQueue = queue.size();234Please respect copyright.PENANAm51jQaccjE
for (int j = 0; j < nodesInQueue; j++) {234Please respect copyright.PENANAtaNdoFEgp3
String node = queue.remove();234Please respect copyright.PENANAzWAfb3ksHq
// 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)) {234Please respect copyright.PENANAIzmaRI62Q7
return steps;234Please respect copyright.PENANAjUjhVYoCBF
}
for (char c: new char[] {'A', 'C', 'G', 'T'}) {234Please respect copyright.PENANAMKw0a5Juvx
for (int i = 0; i < node.length(); i++) {234Please respect copyright.PENANAhCVccatYnw
String neighbor = node.substring(0, i) + c + node.substring(i + 1);234Please respect copyright.PENANA8HnmQtMMVj
if (!seen.contains(neighbor) && Arrays.asList(bank).contains(neighbor)) {234Please respect copyright.PENANAWXyQTWqhdh
queue.add(neighbor);234Please respect copyright.PENANAtJ3LeRjIq0
seen.add(neighbor);234Please respect copyright.PENANAw3mp7nF7Kb
}234Please respect copyright.PENANAmruLMaEmUc
}234Please respect copyright.PENANAkIApAX6fW2
}234Please respect copyright.PENANAhgOb6UaXnn
}234Please respect copyright.PENANAW0iyWy0oEM
234Please respect copyright.PENANAqFG1y0pAM8
steps++;234Please respect copyright.PENANAICXTgr74FW
}234Please respect copyright.PENANAgRulEnUhHa
// If we finish the BFS and did not find end, return -1.234Please respect copyright.PENANAtjVNV0tpYe
return -1;234Please respect copyright.PENANATHC6CyC87D
}234Please respect copyright.PENANAiBT0asf7xb
}