# 433. Minimum Genetic Mutation

**<font color=red>难度: Medium</font>**

## 刷题内容

> 原题连接

* https://leetcode.com/problems/minimum-genetic-mutation/

> 内容描述

```
A gene string can be represented by an 8-character long string, with choices from "A", "C", "G", "T".

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.

For example, "AACCGGTT" -> "AACCGGTA" is 1 mutation.

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.

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.

Note:

Starting point is assumed to be valid, so it might not be included in the bank.
If multiple mutations are needed, all mutations during in the sequence must be valid.
You may assume start and end string is not the same.
 

Example 1:

start: "AACCGGTT"
end:   "AACCGGTA"
bank: ["AACCGGTA"]

return: 1
 

Example 2:

start: "AACCGGTT"
end:   "AAACGGTA"
bank: ["AACCGGTA", "AACCGCTA", "AAACGGTA"]

return: 2
 

Example 3:

start: "AAAAACCC"
end:   "AACCCCCC"
bank: ["AAAACCCC", "AAACCCCC", "AACCCCCC"]

return: 3
```

## 解题方案

> 思路 1
******- 时间复杂度: O(len(genes)^len(start))******- 空间复杂度: O(len(genes)^len(start))******

DFS, beats 27.99%

```python
class Solution:
    def minMutation(self, start: 'str', end: 'str', bank: 'List[str]') -> 'int':
        self.res = sys.maxsize
        self.genes = 'ACGT'
        
        def dfs(s, step, visited):
            if s == end:
                self.res = min(self.res, step)
            visited.add(s)
            for i in range(len(s)):
                for gene in self.genes:
                    if gene != s[i]:
                        nxt = s[:i] + gene + s[i+1:]
                        if nxt in bank and nxt not in visited:
                            dfs(nxt, step + 1, set(_ for _ in visited))
                            
        dfs(start, 0, set())
        return self.res if self.res != sys.maxsize else -1
```

> 思路 2
******- 时间复杂度: O(len(genes)^len(start))******- 空间复杂度: O(len(genes)^len(start))******

BFS，求最优解等比较快，需要记录状态

```bank.remove(next)```是因为：如果说str1是成功道路上的必经之点，那么如果访问到了就会返回正确结果，需要remove掉，如果还没有访问到，说明我们也还没有到正确道路。然后如果有好几种成功道路的话，str1访问到了也还是会返回正确结果即最优解了

```python
class Solution(object):
    def minMutation(self, start, end, bank):
        """
        :type start: str
        :type end: str
        :type bank: List[str]
        :rtype: int
        """
        self.genes = 'ACGT'
        stack, bank = collections.deque([(start,0)]), set(bank)
        while stack:
            cur, step = stack.popleft()
            if cur == end:
                return step
            for i in range(len(cur)):
                for gene in self.genes:
                    if gene != cur[i]:
                        next = cur[:i] + gene + cur[i+1:]
                        if next in bank:
                            bank.remove(next)
                            stack.append((next, step + 1))
        return -1
```





























