
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)273Please respect copyright.PENANAIqJPAbaoev
// better than use DFS as it just need to find out the shortest path.
class Solution {273Please respect copyright.PENANAtw6TvsrcvL
public int minMutation(String start, String end, String[] bank) {273Please respect copyright.PENANAaonqBrcRdk
// 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.273Please respect copyright.PENANAzlqHpjzl1H
Queue<String> queue = new LinkedList<>();273Please respect copyright.PENANACbKSQ5M2WA
Set<String> seen = new HashSet<>();273Please respect copyright.PENANAvbSYkI3EwK
queue.add(start);273Please respect copyright.PENANAmVOiatffch
seen.add(start);273Please respect copyright.PENANAZ6uRIsCmfF
273Please respect copyright.PENANAo6uyDnq4Lb
int steps = 0;273Please respect copyright.PENANAkPIUAPxY7u
273Please respect copyright.PENANAY4vTzqjfdz
while (!queue.isEmpty()) {273Please respect copyright.PENANAaN6PGWrLE5
int nodesInQueue = queue.size();273Please respect copyright.PENANAX0AWT64F2W
for (int j = 0; j < nodesInQueue; j++) {273Please respect copyright.PENANAC10velJio6
String node = queue.remove();273Please respect copyright.PENANACg9lgSYIju
// 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)) {273Please respect copyright.PENANAzUZy7YK8UX
return steps;273Please respect copyright.PENANAhKuaoQgWsj
}
for (char c: new char[] {'A', 'C', 'G', 'T'}) {273Please respect copyright.PENANAECPwgvQLvZ
for (int i = 0; i < node.length(); i++) {273Please respect copyright.PENANAMXO4Ktu9hF
String neighbor = node.substring(0, i) + c + node.substring(i + 1);273Please respect copyright.PENANAwD006nhi23
if (!seen.contains(neighbor) && Arrays.asList(bank).contains(neighbor)) {273Please respect copyright.PENANAUQKRoVPFZl
queue.add(neighbor);273Please respect copyright.PENANAzPFq1dZP5l
seen.add(neighbor);273Please respect copyright.PENANAZlJ73TRGUu
}273Please respect copyright.PENANAitlV53sKQH
}273Please respect copyright.PENANAlPA77BCcjH
}273Please respect copyright.PENANA9RYCL9JqCS
}273Please respect copyright.PENANAv6FZyVId0p
273Please respect copyright.PENANAMDtCPJZ7fy
steps++;273Please respect copyright.PENANAMlw8mj7wdZ
}273Please respect copyright.PENANA6xLZXoGQkq
// If we finish the BFS and did not find end, return -1.273Please respect copyright.PENANAe5VSAP5Wo0
return -1;273Please respect copyright.PENANAR2B5GeGxqJ
}273Please respect copyright.PENANAIu2Cc6s9CV
}