
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)194Please respect copyright.PENANAdXLjGPUx6Y
// better than use DFS as it just need to find out the shortest path.
class Solution {194Please respect copyright.PENANAOYPD25dXH8
public int minMutation(String start, String end, String[] bank) {194Please respect copyright.PENANA1gng6Pjiez
// 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.194Please respect copyright.PENANAveX4zBioja
Queue<String> queue = new LinkedList<>();194Please respect copyright.PENANAn4nckjZ2nJ
Set<String> seen = new HashSet<>();194Please respect copyright.PENANAEDFBKsoYkC
queue.add(start);194Please respect copyright.PENANA29R6eqaHRy
seen.add(start);194Please respect copyright.PENANAazRZmUDQnX
194Please respect copyright.PENANAnzyWyjkYry
int steps = 0;194Please respect copyright.PENANA3DnhgYZM54
194Please respect copyright.PENANAAMfEIHMRTF
while (!queue.isEmpty()) {194Please respect copyright.PENANApSUTCaRbpO
int nodesInQueue = queue.size();194Please respect copyright.PENANABNl48eVTBK
for (int j = 0; j < nodesInQueue; j++) {194Please respect copyright.PENANAA6jVkw3tuW
String node = queue.remove();194Please respect copyright.PENANAXQw4HvL24o
// 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)) {194Please respect copyright.PENANAMuzXpxqJr2
return steps;194Please respect copyright.PENANAYGm7byAju8
}
for (char c: new char[] {'A', 'C', 'G', 'T'}) {194Please respect copyright.PENANAz6GWmztJQ0
for (int i = 0; i < node.length(); i++) {194Please respect copyright.PENANAAe7KcEsd5J
String neighbor = node.substring(0, i) + c + node.substring(i + 1);194Please respect copyright.PENANAqm1T0qn5mU
if (!seen.contains(neighbor) && Arrays.asList(bank).contains(neighbor)) {194Please respect copyright.PENANA1loEIXrJ6q
queue.add(neighbor);194Please respect copyright.PENANAMIXNYFuoih
seen.add(neighbor);194Please respect copyright.PENANAs4mzlBxH68
}194Please respect copyright.PENANAhRHXLEUP9e
}194Please respect copyright.PENANAEcsxrTg1Gp
}194Please respect copyright.PENANA73G6SowrCp
}194Please respect copyright.PENANAYvTVfYgaXd
194Please respect copyright.PENANAnlvldcz5UN
steps++;194Please respect copyright.PENANAExZRODQrYo
}194Please respect copyright.PENANARyV7l0Vn3Y
// If we finish the BFS and did not find end, return -1.194Please respect copyright.PENANA4YHOr5O4f3
return -1;194Please respect copyright.PENANAZ3bjTrHHXC
}194Please respect copyright.PENANAJIkUUZAlrW
}