Leetcode 433. Minimum Genetic Mutation
A gene string can be represented by an 8-character long string, with choices from "A"
, "C"
, "G"
, "T"
. To change one gene string into another, you can mutate one character at a time. Each mutation must change one character into a different character.
You need to determine the minimum number of mutations needed to transform the start
gene string into the end
gene string. If there is no possible mutation path to transform the start string into the end string, return -1.
Note that intermediate gene strings must be valid gene strings in the bank
.
Input: start = "AACCGGTT", end = "AAACGGTA", bank = ["AACCGGTA", "AACCGCTA", "AAACGGTA"]
Output: 2
Input: start = "AAAAACCC", end = "AACCCCCC", bank = ["AAAACCCC", "AAACCCCC", "AACCCCCC"]
Output: 3
start
and end
are strings of length 8.0 <= bank.length <= 10
.bank[i]
is a string of length 8.start
and end
strings, as well as all strings in the bank
, contain only the characters "A"
, "C"
, "G"
, and "T"
?Let’s assume the answers to the above questions are as follows:
To solve this problem, a Breadth-First Search (BFS) approach is most suitable. This is because BFS is great for finding the shortest path in an unweighted graph, which matches our goal of finding the minimum number of mutations.
Here are the steps we’ll take:
end
gene is not in the bank, return -1 because it’s impossible to reach the end.start
gene.end
gene, return the number of mutations.import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;
public class Solution {
public int minMutation(String start, String end, String[] bank) {
Set<String> bankSet = new HashSet<>();
for (String gene : bank) {
bankSet.add(gene);
}
if (!bankSet.contains(end)) {
return -1;
}
char[] charSet = new char[]{'A', 'C', 'G', 'T'};
Queue<String> queue = new LinkedList<>();
queue.offer(start);
Set<String> visited = new HashSet<>();
visited.add(start);
int level = 0;
while (!queue.isEmpty()) {
int size = queue.size();
while (size-- > 0) {
String current = queue.poll();
if (current.equals(end)) {
return level;
}
char[] currentArray = current.toCharArray();
for (int i = 0; i < currentArray.length; i++) {
char originalChar = currentArray[i];
for (char c : charSet) {
if (c != originalChar) {
currentArray[i] = c;
String mutatedGene = new String(currentArray);
if (bankSet.contains(mutatedGene) && !visited.contains(mutatedGene)) {
queue.offer(mutatedGene);
visited.add(mutatedGene);
}
}
}
currentArray[i] = originalChar;
}
}
level++;
}
return -1;
}
}
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?