433. Minimum Genetic Mutation
Minimum Genetic Mutation
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.
Example 1:
Input: start = "AACCGGTT", end = "AACCGGTA", bank = ["AACCGGTA"] Output: 1
Example 2:
Input: start = "AACCGGTT", end = "AAACGGTA", bank = ["AACCGGTA","AACCGCTA","AAACGGTA"] Output: 2
Example 3:
Input: start = "AAAAACCC", end = "AACCCCCC", bank = ["AAAACCCC","AAACCCCC","AACCCCCC"] Output: 3
Constraints:
start.length == 8
end.length == 8
0 <= bank.length <= 10
bank[i].length == 8
start
,end
, andbank[i]
consist of only the characters['A', 'C', 'G', 'T']
.
var minMutation = function(start, end, bank) {
let banks = new Set(bank);
if( !banks.has(end) ) return -1;
let chars = ['A', 'T', 'C', 'G'];
var Q = [[start,0]];
var item, dist, i, j;
while( Q.length > 0 ) {
[item,dist] = Q.shift();
if( item == end ) return dist;
for( i=0; i<8; i++ ) {
for( j=0; j<4; j++ ) {
if( item[i] == chars[j] ) continue;
var node = item.slice(0,i) + chars[j] + item.slice(i+1);
if( banks.has(node) ) {
Q.push([node,dist+1]);
banks.delete(node);
}
}
}
}
return -1;
};
/**
* @param {string} start
* @param {string} end
* @param {string[]} bank
* @return {number}
*/
var minMutation = function(start, end, bank) {
var set = new Set(bank);
if(!set.has(end)) return -1;
var visited = new Set();
visited.add(start);
var q = [];
q.push(start);
var count = 1;
while(q.length!==0){
var size = q.length;
for(var i = 0;i<size;i++){
var a = q.shift();
if(oneM(a,end)) return count;
set.forEach((b)=>{
if(!visited.has(b) && oneM(a,b)){
q.push(b);
visited.add(b);
}
});
}
count++;
}
return -1;
};
var oneM = function(a,b){
var count = 0;
for(var i =0;i<a.length;i++){
if(a[i]!==b[i]){
count++;
}
}
return count===1;
};
/**
* @param {string} start
* @param {string} end
* @param {string[]} bank
* @return {number}
*/
var minMutation = function(start, end, bank) {
var set = new Set(bank);
var visited = new Set();
visited.add(start);
if(!set.has(end)) return -1;
var count = dfs(start);
if(count === Number.MAX_VALUE) return -1;
return count;
//@ return min_path
function dfs(a){
if(oneM(a,end)) return 1;
var min = Number.MAX_VALUE;
set.forEach((b)=>{
if(oneM(a,b) && !visited.has(b)){
visited.add(b);
var c = dfs(b);
if(c!==Number.MAX_VALUE){
min = Math.min(min,c+1);
}
visited.delete(b);
}
});
return min;
}
};
var oneM = function(a,b){
var count = 0;
for(var i =0;i<a.length;i++){
if(a[i]!==b[i]){
count++;
}
}
return count===1;
};
Conclusion
That’s all folks! In this post, we solved LeetCode problem #433. Minimum Genetic Mutation
I hope you have enjoyed this post. Feel free to share your thoughts on this.
You can find the complete source code on my GitHub repository. If you like what you learn. feel free to fork 🔪 and star ⭐ it.
In this blog, I have tried to collect & present the most important points to consider when improving Data structure and logic, feel free to add, edit, comment, or ask. For more information please reach me here
Happy coding!
Comments
Post a Comment