
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)266Please respect copyright.PENANAfdzEkVzaxO
// better than use DFS as it just need to find out the shortest path.
class Solution {266Please respect copyright.PENANAiuRmoGXtbw
public int minMutation(String start, String end, String[] bank) {266Please respect copyright.PENANAyPOQH5lyZ6
// 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.266Please respect copyright.PENANAsGtzoYgIql
Queue<String> queue = new LinkedList<>();266Please respect copyright.PENANAiG0iXsbrcT
Set<String> seen = new HashSet<>();266Please respect copyright.PENANA0t52TaixYL
queue.add(start);266Please respect copyright.PENANA0993ouQlCz
seen.add(start);266Please respect copyright.PENANATpC4UKsFjt
266Please respect copyright.PENANAL9lhwG1QcU
int steps = 0;266Please respect copyright.PENANAPTk6riIAEm
266Please respect copyright.PENANAAZYTu1B0Br
while (!queue.isEmpty()) {266Please respect copyright.PENANAPs1tZIRLYs
int nodesInQueue = queue.size();266Please respect copyright.PENANAg8DOBeK9UM
for (int j = 0; j < nodesInQueue; j++) {266Please respect copyright.PENANATX01xoBczv
String node = queue.remove();266Please respect copyright.PENANAROsq9mk3kW
// 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)) {266Please respect copyright.PENANAuI4q0cGUuJ
return steps;266Please respect copyright.PENANAYkjJJmmFQO
}
for (char c: new char[] {'A', 'C', 'G', 'T'}) {266Please respect copyright.PENANAVkIBEHWP27
for (int i = 0; i < node.length(); i++) {266Please respect copyright.PENANA7nb00jR6bW
String neighbor = node.substring(0, i) + c + node.substring(i + 1);266Please respect copyright.PENANAfuIpXFCctP
if (!seen.contains(neighbor) && Arrays.asList(bank).contains(neighbor)) {266Please respect copyright.PENANAZ4vzdHA91Y
queue.add(neighbor);266Please respect copyright.PENANAxIZwspnhCS
seen.add(neighbor);266Please respect copyright.PENANAWmkb8zvxxP
}266Please respect copyright.PENANA6WGk8vk12S
}266Please respect copyright.PENANAPWc9dLXLbM
}266Please respect copyright.PENANABhQ0PwFdvc
}266Please respect copyright.PENANAAjRYQRydme
266Please respect copyright.PENANAqNcFYaFX26
steps++;266Please respect copyright.PENANASF4hPoETTy
}266Please respect copyright.PENANAzWw2xdQS4A
// If we finish the BFS and did not find end, return -1.266Please respect copyright.PENANAbajG5VxJLG
return -1;266Please respect copyright.PENANAIHchPNhDfA
}266Please respect copyright.PENANAkkn9XiD6Cv
}