
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)276Please respect copyright.PENANAeOtVndIJBb
// better than use DFS as it just need to find out the shortest path.
class Solution {276Please respect copyright.PENANAFYP7TVqa2D
public int minMutation(String start, String end, String[] bank) {276Please respect copyright.PENANAYOfX5IsybA
// 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.276Please respect copyright.PENANAALvGqXMdCe
Queue<String> queue = new LinkedList<>();276Please respect copyright.PENANAJecwwfFbn2
Set<String> seen = new HashSet<>();276Please respect copyright.PENANAaMrPALpeTS
queue.add(start);276Please respect copyright.PENANAsGfXIupMJk
seen.add(start);276Please respect copyright.PENANAK3rZNKhryZ
276Please respect copyright.PENANAnABC56AZ00
int steps = 0;276Please respect copyright.PENANAC68MhLMIAU
276Please respect copyright.PENANAfcAzNTp9B3
while (!queue.isEmpty()) {276Please respect copyright.PENANAGCDloGCq9w
int nodesInQueue = queue.size();276Please respect copyright.PENANAMVXSxyP8cJ
for (int j = 0; j < nodesInQueue; j++) {276Please respect copyright.PENANAWEj96URSZ8
String node = queue.remove();276Please respect copyright.PENANAN0pnVaCtKO
// 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)) {276Please respect copyright.PENANAk8kxy0D1Nk
return steps;276Please respect copyright.PENANAQ9Tabw3CN0
}
for (char c: new char[] {'A', 'C', 'G', 'T'}) {276Please respect copyright.PENANAwdB5mwrqvr
for (int i = 0; i < node.length(); i++) {276Please respect copyright.PENANAnsqJ2W93Gy
String neighbor = node.substring(0, i) + c + node.substring(i + 1);276Please respect copyright.PENANAcWY15DlPYp
if (!seen.contains(neighbor) && Arrays.asList(bank).contains(neighbor)) {276Please respect copyright.PENANAB3Fvj4wnFx
queue.add(neighbor);276Please respect copyright.PENANAiQTGN3AzjP
seen.add(neighbor);276Please respect copyright.PENANA8SPLbwzczk
}276Please respect copyright.PENANAtVIhWwEAdQ
}276Please respect copyright.PENANA8VJDVq0dYR
}276Please respect copyright.PENANAzDufqVXKn4
}276Please respect copyright.PENANAv0TegwOFSS
276Please respect copyright.PENANAUkSDlYAYvh
steps++;276Please respect copyright.PENANAsf7GIQn4NZ
}276Please respect copyright.PENANAGmtfsGPgPw
// If we finish the BFS and did not find end, return -1.276Please respect copyright.PENANAexl2R5Sg73
return -1;276Please respect copyright.PENANA4MwJFQziFy
}276Please respect copyright.PENANA4HUi1Ps8MR
}