-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path0433-minimum-genetic-mutation.js
57 lines (52 loc) · 1.67 KB
/
0433-minimum-genetic-mutation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* 433. Minimum Genetic Mutation
* https://leetcode.com/problems/minimum-genetic-mutation/
* Difficulty: 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.
*/
/**
* @param {string} startGene
* @param {string} endGene
* @param {string[]} bank
* @return {number}
*/
var minMutation = function(startGene, endGene, bank) {
const set = new Set(bank);
if (!set.has(endGene)) return -1;
const queue = [[startGene, 0]];
const seen = new Set([startGene]);
while (queue.length) {
const [gene, steps] = queue.shift();
if (gene === endGene) return steps;
for (let i = 0; i < 8; i++) {
for (const char of ['A', 'C', 'G', 'T']) {
if (char !== gene[i]) {
const next = gene.slice(0, i) + char + gene.slice(i + 1);
if (set.has(next) && !seen.has(next)) {
queue.push([next, steps + 1]);
seen.add(next);
}
}
}
}
}
return -1;
};