
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)220Please respect copyright.PENANA06HzfKh9QV
// better than use DFS as it just need to find out the shortest path.
class Solution {220Please respect copyright.PENANAaqOr9aTr1Y
public int minMutation(String start, String end, String[] bank) {220Please respect copyright.PENANAocvjx1JplV
// 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.220Please respect copyright.PENANAqNGkivhpqu
Queue<String> queue = new LinkedList<>();220Please respect copyright.PENANAdXMg7Yajxl
Set<String> seen = new HashSet<>();220Please respect copyright.PENANAunWWU01Mel
queue.add(start);220Please respect copyright.PENANALlcS4taUTh
seen.add(start);220Please respect copyright.PENANAZJ34crXcgk
220Please respect copyright.PENANAqK7SattPB1
int steps = 0;220Please respect copyright.PENANAuFJa0z18M4
220Please respect copyright.PENANA1nhOgOny4z
while (!queue.isEmpty()) {220Please respect copyright.PENANAaEMVh9EINd
int nodesInQueue = queue.size();220Please respect copyright.PENANAxSqTmE8ZKj
for (int j = 0; j < nodesInQueue; j++) {220Please respect copyright.PENANAoa0swq8Ek4
String node = queue.remove();220Please respect copyright.PENANAaIy6q1MIf6
// 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)) {220Please respect copyright.PENANA1MIW8eGClO
return steps;220Please respect copyright.PENANA0QKLBL9bWs
}
for (char c: new char[] {'A', 'C', 'G', 'T'}) {220Please respect copyright.PENANA4D7yqbdjPR
for (int i = 0; i < node.length(); i++) {220Please respect copyright.PENANADxjFnJDTyl
String neighbor = node.substring(0, i) + c + node.substring(i + 1);220Please respect copyright.PENANA4y8Lt0nDaM
if (!seen.contains(neighbor) && Arrays.asList(bank).contains(neighbor)) {220Please respect copyright.PENANAL7cszX3j4o
queue.add(neighbor);220Please respect copyright.PENANA5klC3MgSoE
seen.add(neighbor);220Please respect copyright.PENANAOE0mhEBmbW
}220Please respect copyright.PENANAfEM6eltQnB
}220Please respect copyright.PENANAf8L7lX5Eje
}220Please respect copyright.PENANAgfiNORYv92
}220Please respect copyright.PENANAwshKW4miqL
220Please respect copyright.PENANAO3H4iM5Oya
steps++;220Please respect copyright.PENANAY9tua4eRAo
}220Please respect copyright.PENANAXAsmvUuCGq
// If we finish the BFS and did not find end, return -1.220Please respect copyright.PENANABIwPVz64rj
return -1;220Please respect copyright.PENANA6mwR8BXVSE
}220Please respect copyright.PENANA032oUK6zuG
}