
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)256Please respect copyright.PENANAXZ6O3d1Y15
// better than use DFS as it just need to find out the shortest path.
class Solution {256Please respect copyright.PENANAXiu0NHxhGt
public int minMutation(String start, String end, String[] bank) {256Please respect copyright.PENANAlnDUFB0SUO
// 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.256Please respect copyright.PENANAbNarROohaw
Queue<String> queue = new LinkedList<>();256Please respect copyright.PENANAVH8cgPTcbA
Set<String> seen = new HashSet<>();256Please respect copyright.PENANA6olEMjQ8ft
queue.add(start);256Please respect copyright.PENANA5jqhu2VTf4
seen.add(start);256Please respect copyright.PENANAFI8MfmGBGH
256Please respect copyright.PENANA37hL6vaN3L
int steps = 0;256Please respect copyright.PENANALl4yvo6THt
256Please respect copyright.PENANAgpCv3zMhgp
while (!queue.isEmpty()) {256Please respect copyright.PENANAfE0fA0d6xe
int nodesInQueue = queue.size();256Please respect copyright.PENANAGZrwvuLi1n
for (int j = 0; j < nodesInQueue; j++) {256Please respect copyright.PENANAdi8Qv8DMmI
String node = queue.remove();256Please respect copyright.PENANAzyGFRrugLl
// 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)) {256Please respect copyright.PENANA3sCStWJf8V
return steps;256Please respect copyright.PENANA80BAQ1mxbp
}
for (char c: new char[] {'A', 'C', 'G', 'T'}) {256Please respect copyright.PENANAAfSzGuxRKW
for (int i = 0; i < node.length(); i++) {256Please respect copyright.PENANAObXQUs5BlN
String neighbor = node.substring(0, i) + c + node.substring(i + 1);256Please respect copyright.PENANAlilCS4aWfa
if (!seen.contains(neighbor) && Arrays.asList(bank).contains(neighbor)) {256Please respect copyright.PENANAYd1UJ6qdHx
queue.add(neighbor);256Please respect copyright.PENANAVhjUqatKlm
seen.add(neighbor);256Please respect copyright.PENANAdEROOvNOht
}256Please respect copyright.PENANAGyF1SBM6rG
}256Please respect copyright.PENANAkytjZKVxFP
}256Please respect copyright.PENANA8uL4PdHvF4
}256Please respect copyright.PENANAaaFB0mP38v
256Please respect copyright.PENANA1V12lDaQOE
steps++;256Please respect copyright.PENANAzWTpy9h6Fx
}256Please respect copyright.PENANAGNSI2jruDI
// If we finish the BFS and did not find end, return -1.256Please respect copyright.PENANAZzKqSFQGPH
return -1;256Please respect copyright.PENANA36AuxxoLrj
}256Please respect copyright.PENANARy1I54sEOp
}