
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)197Please respect copyright.PENANAvX0PootC8u
// better than use DFS as it just need to find out the shortest path.
class Solution {197Please respect copyright.PENANAkDp898OnYW
public int minMutation(String start, String end, String[] bank) {197Please respect copyright.PENANAnfmqPeZNhu
// 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.197Please respect copyright.PENANA63WxDEd5Eh
Queue<String> queue = new LinkedList<>();197Please respect copyright.PENANAPUnfeIRDhm
Set<String> seen = new HashSet<>();197Please respect copyright.PENANAStUqx8ItkO
queue.add(start);197Please respect copyright.PENANAwDfk5oqpjW
seen.add(start);197Please respect copyright.PENANAPamEWkJkGL
197Please respect copyright.PENANAN9cBSO1bcJ
int steps = 0;197Please respect copyright.PENANAt6gP6n5ufo
197Please respect copyright.PENANAFhKlCo6Q0i
while (!queue.isEmpty()) {197Please respect copyright.PENANAdsvaw3JPOS
int nodesInQueue = queue.size();197Please respect copyright.PENANAXnT0rTixH7
for (int j = 0; j < nodesInQueue; j++) {197Please respect copyright.PENANA9sW7GI4PST
String node = queue.remove();197Please respect copyright.PENANASPh02jZmCp
// 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)) {197Please respect copyright.PENANAq3rZAewJDi
return steps;197Please respect copyright.PENANAU7vM1ZrUd9
}
for (char c: new char[] {'A', 'C', 'G', 'T'}) {197Please respect copyright.PENANAeYACDj6P7x
for (int i = 0; i < node.length(); i++) {197Please respect copyright.PENANA4noHFZoIP4
String neighbor = node.substring(0, i) + c + node.substring(i + 1);197Please respect copyright.PENANAWrg6e0sIsL
if (!seen.contains(neighbor) && Arrays.asList(bank).contains(neighbor)) {197Please respect copyright.PENANAtQVXv78xWm
queue.add(neighbor);197Please respect copyright.PENANAqTpsrTmkP7
seen.add(neighbor);197Please respect copyright.PENANAE0iaUupkiI
}197Please respect copyright.PENANA1csjVQlfwW
}197Please respect copyright.PENANAT42BQSVThg
}197Please respect copyright.PENANAcpwYDOaAkC
}197Please respect copyright.PENANAR9GF8Ffm2r
197Please respect copyright.PENANAh7NznypEzn
steps++;197Please respect copyright.PENANAcNQlOxkv7E
}197Please respect copyright.PENANA36CoH6wloW
// If we finish the BFS and did not find end, return -1.197Please respect copyright.PENANAHxBcxorZzu
return -1;197Please respect copyright.PENANAJ5LimQYgPH
}197Please respect copyright.PENANAtCvssqPASB
}