
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)200Please respect copyright.PENANAV6lgrnvvig
// better than use DFS as it just need to find out the shortest path.
class Solution {200Please respect copyright.PENANABB8My2xZCA
public int minMutation(String start, String end, String[] bank) {200Please respect copyright.PENANAJLDmr75N1A
// 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.200Please respect copyright.PENANA3bgIEJQ2U4
Queue<String> queue = new LinkedList<>();200Please respect copyright.PENANApxbegh8PED
Set<String> seen = new HashSet<>();200Please respect copyright.PENANAgYYWNHR6Mt
queue.add(start);200Please respect copyright.PENANANvHxOcWugH
seen.add(start);200Please respect copyright.PENANAp4mL7ZSpab
200Please respect copyright.PENANAM0bSYyNck8
int steps = 0;200Please respect copyright.PENANA9ruzlQBbtv
200Please respect copyright.PENANA7zDuIOFZnO
while (!queue.isEmpty()) {200Please respect copyright.PENANA1XVMGtx4O9
int nodesInQueue = queue.size();200Please respect copyright.PENANA7q9TmSidyx
for (int j = 0; j < nodesInQueue; j++) {200Please respect copyright.PENANAo0jBM8Nazf
String node = queue.remove();200Please respect copyright.PENANAcf0ci5jHtK
// 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)) {200Please respect copyright.PENANAuDYEechVpv
return steps;200Please respect copyright.PENANAoMGS5I6fe5
}
for (char c: new char[] {'A', 'C', 'G', 'T'}) {200Please respect copyright.PENANAaWRTWLbGgo
for (int i = 0; i < node.length(); i++) {200Please respect copyright.PENANA6vNv5LEmSX
String neighbor = node.substring(0, i) + c + node.substring(i + 1);200Please respect copyright.PENANATQ7nqIpMta
if (!seen.contains(neighbor) && Arrays.asList(bank).contains(neighbor)) {200Please respect copyright.PENANALu37Ou9OYS
queue.add(neighbor);200Please respect copyright.PENANAVodaSyH4D8
seen.add(neighbor);200Please respect copyright.PENANAT5IrCYq6H1
}200Please respect copyright.PENANAuHiEbsoUHV
}200Please respect copyright.PENANABTZy49occO
}200Please respect copyright.PENANAT1SqR4s0VI
}200Please respect copyright.PENANAZjrhfPC5rZ
200Please respect copyright.PENANAuSVG7MnxPa
steps++;200Please respect copyright.PENANAoACbYwusZ0
}200Please respect copyright.PENANAtykgZ0XhYj
// If we finish the BFS and did not find end, return -1.200Please respect copyright.PENANAjzG2h8O6fn
return -1;200Please respect copyright.PENANAnFDCsNXAPC
}200Please respect copyright.PENANAleW1ekzO1v
}