
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)299Please respect copyright.PENANAmYHN65JZni
// better than use DFS as it just need to find out the shortest path.
class Solution {299Please respect copyright.PENANAxP49QkS7DU
public int minMutation(String start, String end, String[] bank) {299Please respect copyright.PENANAlzN7C6SXgq
// 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.299Please respect copyright.PENANAezTxSU1f0h
Queue<String> queue = new LinkedList<>();299Please respect copyright.PENANAzFuaX5YDku
Set<String> seen = new HashSet<>();299Please respect copyright.PENANAXFV6BOGdBq
queue.add(start);299Please respect copyright.PENANAnx1wAHtkse
seen.add(start);299Please respect copyright.PENANA9JLQHE3r3s
299Please respect copyright.PENANAFZggbzCrOC
int steps = 0;299Please respect copyright.PENANAKEBFwXi98k
299Please respect copyright.PENANAwGnlnoJAkG
while (!queue.isEmpty()) {299Please respect copyright.PENANApddLBXfWSl
int nodesInQueue = queue.size();299Please respect copyright.PENANAxZ31WgTy1o
for (int j = 0; j < nodesInQueue; j++) {299Please respect copyright.PENANAUNJg1AffFq
String node = queue.remove();299Please respect copyright.PENANA7kYPoqadLD
// 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)) {299Please respect copyright.PENANAzRAMRLKjDc
return steps;299Please respect copyright.PENANAtIMkDZkA2S
}
for (char c: new char[] {'A', 'C', 'G', 'T'}) {299Please respect copyright.PENANAzTdPtPkuu5
for (int i = 0; i < node.length(); i++) {299Please respect copyright.PENANAUgYH9dkYPB
String neighbor = node.substring(0, i) + c + node.substring(i + 1);299Please respect copyright.PENANAqamHlC5noQ
if (!seen.contains(neighbor) && Arrays.asList(bank).contains(neighbor)) {299Please respect copyright.PENANAN4t5v3XIYv
queue.add(neighbor);299Please respect copyright.PENANAtPoERTTx1a
seen.add(neighbor);299Please respect copyright.PENANA0PxUcGz2lh
}299Please respect copyright.PENANAUhAPMPXlam
}299Please respect copyright.PENANAt1ctsQV4I6
}299Please respect copyright.PENANAuhpUT4bUxD
}299Please respect copyright.PENANAmADuA4srIt
299Please respect copyright.PENANAXxOETWj5Cu
steps++;299Please respect copyright.PENANAQGl922vPDr
}299Please respect copyright.PENANAomvFWTZS5C
// If we finish the BFS and did not find end, return -1.299Please respect copyright.PENANAZItA1MEV54
return -1;299Please respect copyright.PENANAyO9PQUXA6m
}299Please respect copyright.PENANAf1zdh2ubhr
}