
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)198Please respect copyright.PENANAl0zjWYiC01
// better than use DFS as it just need to find out the shortest path.
class Solution {198Please respect copyright.PENANAtezZLSwje7
public int minMutation(String start, String end, String[] bank) {198Please respect copyright.PENANAsKVdzG3fsH
// 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.198Please respect copyright.PENANAwnmqmgvbi9
Queue<String> queue = new LinkedList<>();198Please respect copyright.PENANAd1jcXHA65N
Set<String> seen = new HashSet<>();198Please respect copyright.PENANAzus3MU8Qpz
queue.add(start);198Please respect copyright.PENANAkOFM8xlmU7
seen.add(start);198Please respect copyright.PENANA3YIEtk6fkC
198Please respect copyright.PENANAT7lWps887C
int steps = 0;198Please respect copyright.PENANAlaGLIKa3LE
198Please respect copyright.PENANAN2VAa1nIFr
while (!queue.isEmpty()) {198Please respect copyright.PENANAfiSbuuIpmE
int nodesInQueue = queue.size();198Please respect copyright.PENANArV178IwwWh
for (int j = 0; j < nodesInQueue; j++) {198Please respect copyright.PENANA39GhG9wsd5
String node = queue.remove();198Please respect copyright.PENANAduYAeSlNYw
// 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)) {198Please respect copyright.PENANAsvtZHJ2Okc
return steps;198Please respect copyright.PENANAGmyQUa9uxj
}
for (char c: new char[] {'A', 'C', 'G', 'T'}) {198Please respect copyright.PENANAhdUAsnO2s0
for (int i = 0; i < node.length(); i++) {198Please respect copyright.PENANAPgihg3srk4
String neighbor = node.substring(0, i) + c + node.substring(i + 1);198Please respect copyright.PENANArXDpX5WzNN
if (!seen.contains(neighbor) && Arrays.asList(bank).contains(neighbor)) {198Please respect copyright.PENANABuzzX2ojhn
queue.add(neighbor);198Please respect copyright.PENANAKLSh6GF5gF
seen.add(neighbor);198Please respect copyright.PENANAiIGWyCKYi3
}198Please respect copyright.PENANAgcgTOttLGb
}198Please respect copyright.PENANAiiJxolfR02
}198Please respect copyright.PENANAIUAfDRVHqR
}198Please respect copyright.PENANAtZqTpCskl2
198Please respect copyright.PENANAR0aswFuUG4
steps++;198Please respect copyright.PENANAmjmCpAndZJ
}198Please respect copyright.PENANAPMUJIoYiDS
// If we finish the BFS and did not find end, return -1.198Please respect copyright.PENANAgLALJFSwa2
return -1;198Please respect copyright.PENANAXckDORng1W
}198Please respect copyright.PENANAphdTTLnjTx
}