Skip to content

Commit d926ee6

Browse files
committed
433. Minimum Genetic Mutation
1 parent 7af0f26 commit d926ee6

1 file changed

Lines changed: 135 additions & 0 deletions

File tree

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
# LeetCode 433. Minimum Genetic Mutation
2+
3+
## Description
4+
5+
A gene string can be represented by an 8-character long string, with choices from "A", "C", "G", "T".
6+
7+
Suppose we need to investigate about a mutation (mutation from "start" to "end"), where ONE mutation is defined as ONE single character changed in the gene string.
8+
9+
For example, "AACCGGTT" -> "AACCGGTA" is 1 mutation.
10+
11+
Also, there is a given gene "bank", which records all the valid gene mutations. A gene must be in the bank to make it a valid gene string.
12+
13+
Now, given 3 things - start, end, bank, your task is to determine what is the minimum number of mutations needed to mutate from "start" to "end". If there is no such a mutation, return -1.
14+
15+
Note:
16+
17+
Starting point is assumed to be valid, so it might not be included in the bank.
18+
If multiple mutations are needed, all mutations during in the sequence must be valid.
19+
You may assume start and end string is not the same.
20+
21+
```py
22+
23+
Example 1:
24+
25+
start: "AACCGGTT"
26+
end: "AACCGGTA"
27+
bank: ["AACCGGTA"]
28+
29+
return: 1
30+
31+
32+
Example 2:
33+
34+
start: "AACCGGTT"
35+
end: "AAACGGTA"
36+
bank: ["AACCGGTA", "AACCGCTA", "AAACGGTA"]
37+
38+
return: 2
39+
40+
41+
Example 3:
42+
43+
start: "AAAAACCC"
44+
end: "AACCCCCC"
45+
bank: ["AAAACCCC", "AAACCCCC", "AACCCCCC"]
46+
47+
return: 3
48+
```
49+
50+
## 描述
51+
52+
一条基因序列由一个带有8个字符的字符串表示,其中每个字符都属于 "A", "C", "G", "T"中的任意一个。
53+
54+
假设我们要调查一个基因序列的变化。一次基因变化意味着这个基因序列中的一个字符发生了变化。
55+
56+
例如,基因序列由"AACCGGTT" 变化至 "AACCGGTA" 即发生了一次基因变化。
57+
58+
与此同时,每一次基因变化的结果,都需要是一个合法的基因串,即该结果属于一个基因库。
59+
60+
现在给定3个参数 — start, end, bank,分别代表起始基因序列,目标基因序列及基因库,请找出能够使起始基因序列变化为目标基因序列所需的最少变化次数。如果无法实现目标变化,请返回 -1。
61+
62+
注意:
63+
64+
起始基因序列默认是合法的,但是它并不一定会出现在基因库中。
65+
所有的目标基因序列必须是合法的。
66+
假定起始基因序列与目标基因序列是不一样的。
67+
68+
```py
69+
示例 1:
70+
71+
start: "AACCGGTT"
72+
end: "AACCGGTA"
73+
bank: ["AACCGGTA"]
74+
75+
返回值: 1
76+
示例 2:
77+
78+
start: "AACCGGTT"
79+
end: "AAACGGTA"
80+
bank: ["AACCGGTA", "AACCGCTA", "AAACGGTA"]
81+
82+
返回值: 2
83+
示例 3:
84+
85+
start: "AAAAACCC"
86+
end: "AACCCCCC"
87+
bank: ["AAAACCCC", "AAACCCCC", "AACCCCCC"]
88+
89+
返回值: 3
90+
```
91+
92+
来源:力扣(LeetCode)
93+
链接:https://leetcode-cn.com/problems/minimum-genetic-mutation
94+
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
95+
96+
### 思路
97+
98+
* 使用广度优先遍历,初始字符串为第一层,与初始字符串相差一个字符的字符串为下一层的字符串。
99+
* 遍历寻找字符串,直到找到最重的字符串或者队列为空。
100+
101+
```py
102+
# -*- coding: utf-8 -*-
103+
# @Author: 何睿
104+
# @Create Date: 2020-01-30 11:43:15
105+
# @Last Modified by: 何睿
106+
# @Last Modified time: 2020-01-30 12:11:17
107+
108+
import collections
109+
from typing import List
110+
111+
112+
class Solution:
113+
def _valid_next(self, current: str, next_: str):
114+
# 判断两个长度相等的字符串,对应位置是否只有 1 个字符不同
115+
return sum(1 for c, n in zip(current, next_) if c != n) == 1
116+
117+
def minMutation(self, start: str, end: str, bank: List[str]) -> int:
118+
queue = collections.deque()
119+
queue.append([start, '', 0])
120+
121+
while queue:
122+
# 当前字符串,当前字符串的前一个字符串,steps
123+
current, previous, steps = queue.popleft()
124+
# 当前字符等于目标字符
125+
if current == end:
126+
return steps
127+
# 找到和 current 字符相差一个字符的字符,并且此字符不能和 current 的上一个字符相等
128+
for item in bank:
129+
if item != previous and self._valid_next(current, item):
130+
queue.append([item, current, steps + 1])
131+
132+
return -1
133+
```
134+
源代码文件在 [这里](https://github.com/ruicore/Algorithm/blob/master/LeetCode/2020-01-30-433-Minimum-Genetic-Mutation.py)
135+
©本文首发于 何睿的博客 ,欢迎转载,转载需保留 [文章来源](https://ruicore.cn/leetcode-433-minimum-genetic-mutation/) ,作者信息和本声明.

0 commit comments

Comments
 (0)