
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)294Please respect copyright.PENANAheMBt0iAtY
// better than use DFS as it just need to find out the shortest path.
class Solution {294Please respect copyright.PENANALJijp53z3i
public int minMutation(String start, String end, String[] bank) {294Please respect copyright.PENANA6jBFXF5Ic6
// 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.294Please respect copyright.PENANACvFFeu2nHH
Queue<String> queue = new LinkedList<>();294Please respect copyright.PENANAcsNfQQcdsv
Set<String> seen = new HashSet<>();294Please respect copyright.PENANAXnD3hDNDVB
queue.add(start);294Please respect copyright.PENANA5FQWYUYop9
seen.add(start);294Please respect copyright.PENANAgJQr2kP8v7
294Please respect copyright.PENANAYZUkRXhIek
int steps = 0;294Please respect copyright.PENANAmqUIjTFTaQ
294Please respect copyright.PENANA2dSLktTSEp
while (!queue.isEmpty()) {294Please respect copyright.PENANANKgHFODFdA
int nodesInQueue = queue.size();294Please respect copyright.PENANAkUp8fYiPjL
for (int j = 0; j < nodesInQueue; j++) {294Please respect copyright.PENANAR8ebEXHjiD
String node = queue.remove();294Please respect copyright.PENANAoHNuYPRFlc
// 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)) {294Please respect copyright.PENANAhqHb2AaiMX
return steps;294Please respect copyright.PENANActzf5H8Ybl
}
for (char c: new char[] {'A', 'C', 'G', 'T'}) {294Please respect copyright.PENANAH8bJlHKpR5
for (int i = 0; i < node.length(); i++) {294Please respect copyright.PENANAoSNXzZ4rHO
String neighbor = node.substring(0, i) + c + node.substring(i + 1);294Please respect copyright.PENANAkTLmXZi7RJ
if (!seen.contains(neighbor) && Arrays.asList(bank).contains(neighbor)) {294Please respect copyright.PENANAnGwxtjU1zg
queue.add(neighbor);294Please respect copyright.PENANA5iecCP4W0H
seen.add(neighbor);294Please respect copyright.PENANAv7prinkWhK
}294Please respect copyright.PENANAms2eIldQxb
}294Please respect copyright.PENANAxU11kZnep6
}294Please respect copyright.PENANAOft3jH3XhI
}294Please respect copyright.PENANAyvk7M1CDg9
294Please respect copyright.PENANAy31ZUrm5Il
steps++;294Please respect copyright.PENANAL8DItmMVBg
}294Please respect copyright.PENANAPFCFZiVg84
// If we finish the BFS and did not find end, return -1.294Please respect copyright.PENANADvEFYvlwPk
return -1;294Please respect copyright.PENANAd2FVgmw6hd
}294Please respect copyright.PENANAPqO5f8GuMA
}