433. Minimum Genetic Mutation¶
Difficulty: Medium
LeetCode Problem View on GitHub
433. Minimum Genetic Mutation
Medium
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 startGene to a gene string endGene 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 startGene and endGene and the gene bank bank, return the minimum number of mutations needed to mutate from startGene to endGene. 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.
Example 1:
Input: startGene = "AACCGGTT", endGene = "AACCGGTA", bank = ["AACCGGTA"] Output: 1
Example 2:
Input: startGene = "AACCGGTT", endGene = "AAACGGTA", bank = ["AACCGGTA","AACCGCTA","AAACGGTA"] Output: 2
Constraints:
0 <= bank.length <= 10startGene.length == endGene.length == bank[i].length == 8startGene,endGene, andbank[i]consist of only the characters['A', 'C', 'G', 'T'].
Solution¶
class Solution {
static class Pair {
String s;
int count;
public Pair(String s, int count) {
this.s = s;
this.count = count;
}
@Override
public String toString() {
return "(" + s + " " + count + ")";
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Pair current = (Pair)(obj);
return current.s == s && current.count == count;
}
@Override
public int hashCode() {
return Objects.hash(s, count);
}
}
public int minMutation(String startGene, String endGene, String[] bank) {
if (startGene.length() != endGene.length()) return -1;
HashSet<String> set = new HashSet<>();
for (String x : bank) set.add(x);
if (!set.contains(endGene)) return -1;
HashSet<String> vis = new HashSet<>();
vis.add(startGene);
Queue<Pair> q = new LinkedList<>();
q.offer(new Pair(startGene, 0));
int mini = Integer.MAX_VALUE;
while (q.size() > 0) {
Pair current = q.poll();
if (vis.contains(current)) continue;
if (current.s.equals(endGene)) {
mini = Math.min(mini, current.count);
continue;
}
char new_current[] = current.s.toCharArray();
String temp = "ACGT";
for (int i = 0; i < current.s.length(); i++) {
char c = current.s.charAt(i);
for (int j = 0; j < temp.length(); j++) {
new_current[i] = temp.charAt(j);
String store = "";
for (int x = 0; x < new_current.length; x++) store += new_current[x];
if (set.contains(store)) {
if (!vis.contains(store)) q.offer(new Pair(store, current.count + 1));
vis.add(store);
}
new_current[i] = c;
}
}
}
if (mini == Integer.MAX_VALUE) return -1;
return mini;
}
}
Complexity Analysis¶
- Time Complexity:
O(?) - Space Complexity:
O(?)
Approach¶
Detailed explanation of the approach will be added here