
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)204Please respect copyright.PENANAwoxLTC7qL5
// better than use DFS as it just need to find out the shortest path.
class Solution {204Please respect copyright.PENANAAIVOqTbLgS
public int minMutation(String start, String end, String[] bank) {204Please respect copyright.PENANAqyRUfiP2PB
// 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.204Please respect copyright.PENANA9jEW7PkWPJ
Queue<String> queue = new LinkedList<>();204Please respect copyright.PENANAD0Inyj4dls
Set<String> seen = new HashSet<>();204Please respect copyright.PENANAyLVAfSbQga
queue.add(start);204Please respect copyright.PENANAkTeQXoP8BY
seen.add(start);204Please respect copyright.PENANA7GdzlvMvV4
204Please respect copyright.PENANAiKlkUuauPG
int steps = 0;204Please respect copyright.PENANAl9RDWu7FqJ
204Please respect copyright.PENANA0Vxqg8G0Uj
while (!queue.isEmpty()) {204Please respect copyright.PENANAYSrDe3123q
int nodesInQueue = queue.size();204Please respect copyright.PENANAJisevmBnj7
for (int j = 0; j < nodesInQueue; j++) {204Please respect copyright.PENANAmJkPLhfRt1
String node = queue.remove();204Please respect copyright.PENANArpLpwyk2jE
// 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)) {204Please respect copyright.PENANAoVkpqj6Fb5
return steps;204Please respect copyright.PENANAqesaY2037a
}
for (char c: new char[] {'A', 'C', 'G', 'T'}) {204Please respect copyright.PENANAaeFsQqRSJl
for (int i = 0; i < node.length(); i++) {204Please respect copyright.PENANAhnGtngKICX
String neighbor = node.substring(0, i) + c + node.substring(i + 1);204Please respect copyright.PENANAGvM9k5mvg8
if (!seen.contains(neighbor) && Arrays.asList(bank).contains(neighbor)) {204Please respect copyright.PENANA2L1hN9VViy
queue.add(neighbor);204Please respect copyright.PENANAT6NNBDVWhD
seen.add(neighbor);204Please respect copyright.PENANA5wt6ObLAeD
}204Please respect copyright.PENANAme1O8aVJIr
}204Please respect copyright.PENANAMqC7zK989f
}204Please respect copyright.PENANAzzlTeRpv1M
}204Please respect copyright.PENANAlCxP4PJ7Ht
204Please respect copyright.PENANAKEK6l2Mpq1
steps++;204Please respect copyright.PENANA5xV2TCHS8n
}204Please respect copyright.PENANAP8JEb05CCe
// If we finish the BFS and did not find end, return -1.204Please respect copyright.PENANA3g5I9utfKJ
return -1;204Please respect copyright.PENANAUIqE1A6PUJ
}204Please respect copyright.PENANAFxp8wPjYgr
}