Skip to content

Commit b3229d9

Browse files
author
谭佳胜
committed
Minimum Genetic Mutation
1 parent a348ec1 commit b3229d9

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const diff = function(str1, str2) {
2+
if (str1 === str2) return 0;
3+
4+
let count = 0;
5+
6+
for (let i = 0; i < str1.length; i++) {
7+
if (str1[i] !== str2[i]) count++;
8+
}
9+
10+
return count;
11+
}
12+
13+
const minMutation = function(start, end, bank, depth = 0, result = []) {
14+
if (diff(start, end) === 0) return result.push(depth);
15+
16+
bank.forEach((gen, index) => {
17+
if (diff(start, gen) === 1) minMutation(gen, end, bank.filter((_, i) => i !== index), depth + 1, result);
18+
});
19+
20+
return result.length > 0 ? Math.min(...result) : -1;
21+
};

0 commit comments

Comments
 (0)