
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)237Please respect copyright.PENANAniaLVZtOOW
// better than use DFS as it just need to find out the shortest path.
class Solution {237Please respect copyright.PENANAXAUvSSwQeu
public int minMutation(String start, String end, String[] bank) {237Please respect copyright.PENANAatC3W9oCxB
// 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.237Please respect copyright.PENANAc6BQ7QqyD2
Queue<String> queue = new LinkedList<>();237Please respect copyright.PENANAc9523HkxzR
Set<String> seen = new HashSet<>();237Please respect copyright.PENANATYQUZhAk6F
queue.add(start);237Please respect copyright.PENANAZdi0k8eAaG
seen.add(start);237Please respect copyright.PENANAATbEBFZtTB
237Please respect copyright.PENANAf2RY16Pfzb
int steps = 0;237Please respect copyright.PENANAkzK3uav67o
237Please respect copyright.PENANAiCkqZamsqO
while (!queue.isEmpty()) {237Please respect copyright.PENANAHACIiFIIGN
int nodesInQueue = queue.size();237Please respect copyright.PENANApJev2l8N9q
for (int j = 0; j < nodesInQueue; j++) {237Please respect copyright.PENANAvGfVFF2QC3
String node = queue.remove();237Please respect copyright.PENANANK5fYkfsmh
// 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)) {237Please respect copyright.PENANAB4Iq2kmuWv
return steps;237Please respect copyright.PENANAhunRYLjKS0
}
for (char c: new char[] {'A', 'C', 'G', 'T'}) {237Please respect copyright.PENANA5BPXXMuqEW
for (int i = 0; i < node.length(); i++) {237Please respect copyright.PENANAAPuYKodDRY
String neighbor = node.substring(0, i) + c + node.substring(i + 1);237Please respect copyright.PENANAPc9mZSZLp1
if (!seen.contains(neighbor) && Arrays.asList(bank).contains(neighbor)) {237Please respect copyright.PENANAbgfuf7at8m
queue.add(neighbor);237Please respect copyright.PENANAtIL2hLSFg9
seen.add(neighbor);237Please respect copyright.PENANARyYQYCkxLC
}237Please respect copyright.PENANA8zWf5eNkOX
}237Please respect copyright.PENANAp2w1OormOx
}237Please respect copyright.PENANAAHqjwqNvSu
}237Please respect copyright.PENANARjoqpLr1a8
237Please respect copyright.PENANAm1Zj3QkeW0
steps++;237Please respect copyright.PENANArAhuX2sgZm
}237Please respect copyright.PENANAgLzQWjdqAs
// If we finish the BFS and did not find end, return -1.237Please respect copyright.PENANAz4ozjrk3Kt
return -1;237Please respect copyright.PENANAUdKn4REVeT
}237Please respect copyright.PENANAODJDLquuk0
}