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)89Please respect copyright.PENANAizc2mntBda
// better than use DFS as it just need to find out the shortest path.
class Solution {89Please respect copyright.PENANAmlBA5t8JTN
public int minMutation(String start, String end, String[] bank) {89Please respect copyright.PENANA8UKfG917tR
// 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.89Please respect copyright.PENANAEP4cZ4uIFi
Queue<String> queue = new LinkedList<>();89Please respect copyright.PENANAw7OOS3QWqS
Set<String> seen = new HashSet<>();89Please respect copyright.PENANAzHBecpPMbt
queue.add(start);89Please respect copyright.PENANARecZAF1H0a
seen.add(start);89Please respect copyright.PENANAPCkVEWe9fr
89Please respect copyright.PENANAjyvZSD6158
int steps = 0;89Please respect copyright.PENANAKJYaXUMWnp
89Please respect copyright.PENANA3LMfpIed7o
while (!queue.isEmpty()) {89Please respect copyright.PENANAyJf4fkhy9r
int nodesInQueue = queue.size();89Please respect copyright.PENANAF8FgL4Bkkr
for (int j = 0; j < nodesInQueue; j++) {89Please respect copyright.PENANAROEF6rE49Z
String node = queue.remove();89Please respect copyright.PENANAQKZfEGyeDk
// 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)) {89Please respect copyright.PENANAuBTP0o82py
return steps;89Please respect copyright.PENANAWspM13jYy7
}
for (char c: new char[] {'A', 'C', 'G', 'T'}) {89Please respect copyright.PENANA2kfXLi0vuN
for (int i = 0; i < node.length(); i++) {89Please respect copyright.PENANAV7HutBQdek
String neighbor = node.substring(0, i) + c + node.substring(i + 1);89Please respect copyright.PENANADgkXRjgPKp
if (!seen.contains(neighbor) && Arrays.asList(bank).contains(neighbor)) {89Please respect copyright.PENANANfngiZ2aMq
queue.add(neighbor);89Please respect copyright.PENANAllfIqVWPtr
seen.add(neighbor);89Please respect copyright.PENANAIqDc20c6a5
}89Please respect copyright.PENANAIPQDD1MjUk
}89Please respect copyright.PENANAX2bFRzzOqZ
}89Please respect copyright.PENANAJlrHfYSgXi
}89Please respect copyright.PENANAHmkjB48vGd
89Please respect copyright.PENANAwwG6QcgTO3
steps++;89Please respect copyright.PENANAHJmz6XZWjM
}89Please respect copyright.PENANAvi6qhTZ2Gk
// If we finish the BFS and did not find end, return -1.89Please respect copyright.PENANAE18rYqjr8n
return -1;89Please respect copyright.PENANALbQ557HzNx
}89Please respect copyright.PENANAmpqUjrpKtI
}