
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)240Please respect copyright.PENANAsUDVkLlmlR
// better than use DFS as it just need to find out the shortest path.
class Solution {240Please respect copyright.PENANAOOUpa9H2Da
public int minMutation(String start, String end, String[] bank) {240Please respect copyright.PENANALKXieJmMXg
// 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.240Please respect copyright.PENANAH4yTu0457M
Queue<String> queue = new LinkedList<>();240Please respect copyright.PENANAe8l3wefMOh
Set<String> seen = new HashSet<>();240Please respect copyright.PENANADE75nZI9rz
queue.add(start);240Please respect copyright.PENANApBwCUIfAaw
seen.add(start);240Please respect copyright.PENANAtPtWvIEKEi
240Please respect copyright.PENANASiuoR0M1DS
int steps = 0;240Please respect copyright.PENANAs1gnd68UJW
240Please respect copyright.PENANAVZrGXk8B27
while (!queue.isEmpty()) {240Please respect copyright.PENANANs1isUzPR3
int nodesInQueue = queue.size();240Please respect copyright.PENANAgMGxOHadia
for (int j = 0; j < nodesInQueue; j++) {240Please respect copyright.PENANAnlOEhOkPAN
String node = queue.remove();240Please respect copyright.PENANASSBkqCtGyk
// 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)) {240Please respect copyright.PENANA9OLo4bX4XR
return steps;240Please respect copyright.PENANA9DABekuqYV
}
for (char c: new char[] {'A', 'C', 'G', 'T'}) {240Please respect copyright.PENANAunr765TtKn
for (int i = 0; i < node.length(); i++) {240Please respect copyright.PENANAXLQ2ZQ3WHU
String neighbor = node.substring(0, i) + c + node.substring(i + 1);240Please respect copyright.PENANAXpAsQv1cxQ
if (!seen.contains(neighbor) && Arrays.asList(bank).contains(neighbor)) {240Please respect copyright.PENANAclg0AkfYlG
queue.add(neighbor);240Please respect copyright.PENANA3xf3mW8B43
seen.add(neighbor);240Please respect copyright.PENANAp8ILqpMp0a
}240Please respect copyright.PENANA7SzoYpgkpG
}240Please respect copyright.PENANA0CLtlMtAKi
}240Please respect copyright.PENANAWUcLcPILRE
}240Please respect copyright.PENANAJwEwZUyHTF
240Please respect copyright.PENANAihkAp5Ob3i
steps++;240Please respect copyright.PENANAMrjubLpxRO
}240Please respect copyright.PENANAb011Pf6rWH
// If we finish the BFS and did not find end, return -1.240Please respect copyright.PENANAHPrfa3enC6
return -1;240Please respect copyright.PENANA5gwI9U97dU
}240Please respect copyright.PENANAZhrodOCgcU
}