|
| 1 | +# 433. Minimum Genetic Mutation - Best Practices of LeetCode Solutions |
| 2 | +LeetCode link: [433. Minimum Genetic Mutation](https://leetcode.com/problems/minimum-genetic-mutation), difficulty: **Medium**. |
| 3 | + |
| 4 | +## LeetCode description of "433. Minimum Genetic Mutation" |
| 5 | +A gene string can be represented by an 8-character long string, with choices from `A`, `C`, `G`, and `T`. |
| 6 | + |
| 7 | +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. |
| 8 | + |
| 9 | +* For example, `"AACCGGTT" --> "AACCGGTA"` is one mutation. |
| 10 | + |
| 11 | +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. |
| 12 | + |
| 13 | +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`. |
| 14 | + |
| 15 | +Note that the starting point is assumed to be valid, so it might not be included in the bank. |
| 16 | + |
| 17 | +### [Example 1] |
| 18 | +**Input**: `startGene = "AACCGGTT", endGene = "AACCGGTA", bank = ["AACCGGTA"]` |
| 19 | + |
| 20 | +**Output**: `1` |
| 21 | + |
| 22 | +### [Example 2] |
| 23 | +**Input**: `startGene = "AACCGGTT", endGene = "AAACGGTA", bank = ["AACCGGTA","AACCGCTA","AAACGGTA"]` |
| 24 | + |
| 25 | +**Output**: `2` |
| 26 | + |
| 27 | +### [Constraints] |
| 28 | +- `0 <= bank.length <= 10` |
| 29 | +- `startGene.length == endGene.length == bank[i].length == 8` |
| 30 | +- `startGene`, `endGene`, and `bank[i]` consist of only the characters `['A', 'C', 'G', 'T']`. |
| 31 | + |
| 32 | +## Intuition |
| 33 | +This issue can be solved by **Breadth-First Search**. |
| 34 | + |
| 35 | +### Breadth-First Search |
| 36 | + |
| 37 | + |
| 38 | +* As shown in the figure above, **breadth-first search** can be thought of as visiting vertices in rounds and rounds. Actually, whenever you see a question is about |
| 39 | + getting `shortest` or `least` of something of a graph, `breadth-first search` would probably help. |
| 40 | + |
| 41 | +* `breadth-first search` emphasizes first-in-first-out, so a **queue** is needed. |
| 42 | + |
| 43 | +## Approach |
| 44 | +1. `Breadth-First Search` a graph means traversing **from near to far**, from `circle 1` to `circle N`. Each `circle` is a round of iteration, but we can simplify it by using just 1 round. |
| 45 | +1. So through `Breadth-First Search`, when a word matches `endWord`, the game is over, and we can return the number of **circle** as a result. |
| 46 | + |
| 47 | +## Complexity |
| 48 | +> **N** is the length of `bank`. |
| 49 | +
|
| 50 | +* Time: `O((8 * 4) * N)`. |
| 51 | +* Space: `O(N)`. |
| 52 | + |
| 53 | +## Python |
| 54 | +```python |
| 55 | +class Solution: |
| 56 | + def minMutation(self, start_gene: str, end_gene: str, bank: List[str]) -> int: |
| 57 | + if not end_gene in bank: |
| 58 | + return -1 |
| 59 | + |
| 60 | + self.end_gene = end_gene |
| 61 | + self.bank = set(bank) |
| 62 | + self.queue = deque([start_gene]) |
| 63 | + result = 0 |
| 64 | + |
| 65 | + while self.queue: |
| 66 | + result += 1 |
| 67 | + queue_size = len(self.queue) |
| 68 | + |
| 69 | + for i in range(queue_size): |
| 70 | + gene = self.queue.popleft() |
| 71 | + |
| 72 | + if self.mutate_one(gene): |
| 73 | + return result |
| 74 | + |
| 75 | + return -1 |
| 76 | + |
| 77 | + def mutate_one(self, gene): |
| 78 | + for i in range(len(gene)): |
| 79 | + for char in ['A', 'C', 'G', 'T']: |
| 80 | + if gene[i] == char: |
| 81 | + continue |
| 82 | + |
| 83 | + mutation = f'{gene[:i]}{char}{gene[i + 1:]}' |
| 84 | + |
| 85 | + if mutation == self.end_gene: |
| 86 | + return True |
| 87 | + |
| 88 | + if mutation in self.bank: |
| 89 | + self.queue.append(mutation) |
| 90 | + self.bank.remove(mutation) |
| 91 | +``` |
| 92 | + |
| 93 | +## JavaScript |
| 94 | +```javascript |
| 95 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 96 | +``` |
| 97 | + |
| 98 | +## Java |
| 99 | +```java |
| 100 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 101 | +``` |
| 102 | + |
| 103 | +## C++ |
| 104 | +```cpp |
| 105 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 106 | +``` |
| 107 | + |
| 108 | +## C# |
| 109 | +```c# |
| 110 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 111 | +``` |
| 112 | + |
| 113 | +## Go |
| 114 | +```go |
| 115 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 116 | +``` |
| 117 | + |
| 118 | +## Ruby |
| 119 | +```ruby |
| 120 | +# Welcome to create a PR to complete the code of this language, thanks! |
| 121 | +``` |
| 122 | + |
| 123 | +## C, Kotlin, Swift, Rust or other languages |
| 124 | +``` |
| 125 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 126 | +``` |
0 commit comments