
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)263Please respect copyright.PENANA52EYYtypNe
// better than use DFS as it just need to find out the shortest path.
class Solution {263Please respect copyright.PENANAVrEsm2kEzt
public int minMutation(String start, String end, String[] bank) {263Please respect copyright.PENANAKhS0qctCHF
// 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.263Please respect copyright.PENANAVhsnOS6tUH
Queue<String> queue = new LinkedList<>();263Please respect copyright.PENANAWCaiyTnrQh
Set<String> seen = new HashSet<>();263Please respect copyright.PENANAKpADtuJrr1
queue.add(start);263Please respect copyright.PENANAMONO1cNYwD
seen.add(start);263Please respect copyright.PENANAPutADXpUwC
263Please respect copyright.PENANAO8kuamWc0R
int steps = 0;263Please respect copyright.PENANA1PKDojOD3v
263Please respect copyright.PENANA5Fmh2uxPUD
while (!queue.isEmpty()) {263Please respect copyright.PENANAwGyUlRAZeW
int nodesInQueue = queue.size();263Please respect copyright.PENANAMhWInDI4hN
for (int j = 0; j < nodesInQueue; j++) {263Please respect copyright.PENANA8eprhLJhtZ
String node = queue.remove();263Please respect copyright.PENANA3ajKT3mqbD
// 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)) {263Please respect copyright.PENANAmxMkTOPJA7
return steps;263Please respect copyright.PENANAjIOP3LojOz
}
for (char c: new char[] {'A', 'C', 'G', 'T'}) {263Please respect copyright.PENANABtyqTxHAsJ
for (int i = 0; i < node.length(); i++) {263Please respect copyright.PENANAeZt7T6y3Gb
String neighbor = node.substring(0, i) + c + node.substring(i + 1);263Please respect copyright.PENANAt6uZRNzNw2
if (!seen.contains(neighbor) && Arrays.asList(bank).contains(neighbor)) {263Please respect copyright.PENANAOeOnmkpjCR
queue.add(neighbor);263Please respect copyright.PENANAdNvImgNMS8
seen.add(neighbor);263Please respect copyright.PENANAXS7qfiIaCw
}263Please respect copyright.PENANA4AM0FQEwHz
}263Please respect copyright.PENANAA2tgC32qZB
}263Please respect copyright.PENANAmenlpsKveq
}263Please respect copyright.PENANAyuQEtPyuUu
263Please respect copyright.PENANAOydrc4OUNi
steps++;263Please respect copyright.PENANAy3nXBy1bsk
}263Please respect copyright.PENANARPRJTK33L6
// If we finish the BFS and did not find end, return -1.263Please respect copyright.PENANAAEeppGau0s
return -1;263Please respect copyright.PENANALdLOeAfMj4
}263Please respect copyright.PENANAcN1RE5d2m4
}