Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.0433
Browse files Browse the repository at this point in the history
No.0433.Minimum Genetic Mutation
  • Loading branch information
yanglbme committed Jan 9, 2022
1 parent 80d0720 commit 67886c1
Show file tree
Hide file tree
Showing 36 changed files with 433 additions and 56 deletions.
2 changes: 1 addition & 1 deletion lcci/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# [《程序员面试金典(第 6 版)》系列](https://leetcode-cn.com/problemset/lcci/)
# 《程序员面试金典(第 6 版)》系列

本书是原谷歌资深面试官的经验之作,帮助了许多想要加入脸书、苹果、谷歌等 IT 名企的求职者拿到 Dream offer。本专题的 100+ 编程面试题是在原书基础上精心挑选出来的,帮助你轻松应战 IT 名企技术面试。

Expand Down
2 changes: 1 addition & 1 deletion lcof/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# [《剑指 Offer(第 2 版)》系列](https://leetcode-cn.com/problemset/lcof/)
# 《剑指 Offer(第 2 版)》系列

![](./lcof.png)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

<p><strong>示例 1:</strong></p>

<p><a href="https://assets.leetcode.com/uploads/2018/12/13/160_example_1.png" target="_blank"><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/lcof2/%E5%89%91%E6%8C%87%20Offer%20II%20023.%20%E4%B8%A4%E4%B8%AA%E9%93%BE%E8%A1%A8%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E9%87%8D%E5%90%88%E8%8A%82%E7%82%B9/images/160_example_1.png" style="height: 130px; width: 400px;" /></a></p>
<p><a href="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/lcof2/%E5%89%91%E6%8C%87%20Offer%20II%20023.%20%E4%B8%A4%E4%B8%AA%E9%93%BE%E8%A1%A8%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E9%87%8D%E5%90%88%E8%8A%82%E7%82%B9/images/160_example_1.png" target="_blank"><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/lcof2/%E5%89%91%E6%8C%87%20Offer%20II%20023.%20%E4%B8%A4%E4%B8%AA%E9%93%BE%E8%A1%A8%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E9%87%8D%E5%90%88%E8%8A%82%E7%82%B9/images/160_example_1.png" style="height: 130px; width: 400px;" /></a></p>

<pre>
<strong>输入:</strong>intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
Expand All @@ -30,7 +30,7 @@

<p><strong>示例&nbsp;2:</strong></p>

<p><a href="https://assets.leetcode.com/uploads/2018/12/13/160_example_2.png" target="_blank"><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/lcof2/%E5%89%91%E6%8C%87%20Offer%20II%20023.%20%E4%B8%A4%E4%B8%AA%E9%93%BE%E8%A1%A8%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E9%87%8D%E5%90%88%E8%8A%82%E7%82%B9/images/160_example_2.png" style="height: 136px; width: 350px;" /></a></p>
<p><a href="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/lcof2/%E5%89%91%E6%8C%87%20Offer%20II%20023.%20%E4%B8%A4%E4%B8%AA%E9%93%BE%E8%A1%A8%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E9%87%8D%E5%90%88%E8%8A%82%E7%82%B9/images/160_example_2.png" target="_blank"><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/lcof2/%E5%89%91%E6%8C%87%20Offer%20II%20023.%20%E4%B8%A4%E4%B8%AA%E9%93%BE%E8%A1%A8%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E9%87%8D%E5%90%88%E8%8A%82%E7%82%B9/images/160_example_2.png" style="height: 136px; width: 350px;" /></a></p>

<pre>
<strong>输入:</strong>intersectVal&nbsp;= 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
Expand All @@ -42,7 +42,7 @@

<p><strong>示例&nbsp;3:</strong></p>

<p><a href="https://assets.leetcode.com/uploads/2018/12/13/160_example_3.png" target="_blank"><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/lcof2/%E5%89%91%E6%8C%87%20Offer%20II%20023.%20%E4%B8%A4%E4%B8%AA%E9%93%BE%E8%A1%A8%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E9%87%8D%E5%90%88%E8%8A%82%E7%82%B9/images/160_example_3.png" style="height: 126px; width: 200px;" /></a></p>
<p><a href="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/lcof2/%E5%89%91%E6%8C%87%20Offer%20II%20023.%20%E4%B8%A4%E4%B8%AA%E9%93%BE%E8%A1%A8%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E9%87%8D%E5%90%88%E8%8A%82%E7%82%B9/images/160_example_3.png" target="_blank"><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/lcof2/%E5%89%91%E6%8C%87%20Offer%20II%20023.%20%E4%B8%A4%E4%B8%AA%E9%93%BE%E8%A1%A8%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E9%87%8D%E5%90%88%E8%8A%82%E7%82%B9/images/160_example_3.png" style="height: 126px; width: 200px;" /></a></p>

<pre>
<strong>输入:</strong>intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

<p><strong>示例 1:</strong></p>

<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/05/03/tree.png" style="height: 364px; width: 534px;" /></strong></p>
<p><strong><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/lcof2/%E5%89%91%E6%8C%87%20Offer%20II%20054.%20%E6%89%80%E6%9C%89%E5%A4%A7%E4%BA%8E%E7%AD%89%E4%BA%8E%E8%8A%82%E7%82%B9%E7%9A%84%E5%80%BC%E4%B9%8B%E5%92%8C/images/tree.png" style="height: 364px; width: 534px;" /></strong></p>

<pre>
<strong>输入:</strong>root<strong> </strong>=<strong> </strong>[4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
4 changes: 2 additions & 2 deletions lcof2/剑指 Offer II 077. 链表排序/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<p><strong>示例 1:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2020/09/14/sort_list_1.jpg" style="width: 302px; " /></p>
<p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/lcof2/%E5%89%91%E6%8C%87%20Offer%20II%20077.%20%E9%93%BE%E8%A1%A8%E6%8E%92%E5%BA%8F/images/sort_list_1.jpg" style="width: 302px; " /></p>

<pre>
<b>输入:</b>head = [4,2,1,3]
Expand All @@ -22,7 +22,7 @@

<p><strong>示例 2:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2020/09/14/sort_list_2.jpg" style="width: 402px; " /></p>
<p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/lcof2/%E5%89%91%E6%8C%87%20Offer%20II%20077.%20%E9%93%BE%E8%A1%A8%E6%8E%92%E5%BA%8F/images/sort_list_2.jpg" style="width: 402px; " /></p>

<pre>
<b>输入:</b>head = [-1,5,3,4,0]
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
130 changes: 129 additions & 1 deletion solution/0400-0499/0433.Minimum Genetic Mutation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,150 @@ bank: ["AAAACCCC", "AAACCCCC", "AACCCCCC"]

<!-- 这里可写通用的实现逻辑 -->

BFS 找最值。

<!-- tabs:start -->

### **Python3**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

class Solution:
def minMutation(self, start: str, end: str, bank: List[str]) -> int:
s = set(bank)
q = deque([(start, 0)])
mp = {'A': 'TCG', 'T': 'ACG', 'C': 'ATG', 'G': 'ATC'}
while q:
t, step = q.popleft()
if t == end:
return step
for i, v in enumerate(t):
for j in mp[v]:
next = t[:i] + j + t[i + 1:]
if next in s:
q.append((next, step + 1))
s.remove(next)
return -1
```

### **Java**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```java
class Solution {
public int minMutation(String start, String end, String[] bank) {
Set<String> s = new HashSet<>();
for (String b : bank) {
s.add(b);
}
Map<Character, String> mp = new HashMap<>(4);
mp.put('A', "TCG");
mp.put('T', "ACG");
mp.put('C', "ATG");
mp.put('G', "ATC");
Deque<Pair<String, Integer>> q = new LinkedList<>();
q.offer(new Pair<>(start, 0));
while (!q.isEmpty()) {
Pair<String, Integer> p = q.poll();
String t = p.getKey();
int step = p.getValue();
if (end.equals(t)) {
return step;
}
for (int i = 0; i < t.length(); ++i) {
for (char c : mp.get(t.charAt(i)).toCharArray()) {
String next = t.substring(0, i) + c + t.substring(i + 1);
if (s.contains(next)) {
q.offer(new Pair<>(next, step + 1));
s.remove(next);
}
}
}
}
return -1;
}
}
```

### **C++**

```cpp
class Solution {
public:
int minMutation(string start, string end, vector<string>& bank) {
unordered_set<string> s;
for (auto& b : bank) s.insert(b);
unordered_map<char, string> mp;
mp['A'] = "TCG";
mp['T'] = "ACG";
mp['C'] = "ATG";
mp['G'] = "ATC";
queue<pair<string, int>> q;
q.push({start, 0});
while (!q.empty())
{
auto p = q.front();
q.pop();
string t = p.first;
int step = p.second;
if (t == end) return step;
for (int i = 0; i < t.size(); ++i)
{
for (char c : mp[t[i]])
{
string next = t.substr(0, i) + c + t.substr(i + 1, t.size() - i - 1);
if (s.count(next))
{
q.push({next, step + 1});
s.erase(next);
}
}
}
}
return -1;
}
};
```
### **Go**
```go
func minMutation(start string, end string, bank []string) int {
s := make(map[string]bool)
for _, b := range bank {
s[b] = true
}
mp := make(map[byte]string)
mp['A'] = "TCG"
mp['T'] = "ACG"
mp['C'] = "ATG"
mp['G'] = "ATC"
type pair struct {
first string
second int
}
q := []pair{{start, 0}}
for len(q) > 0 {
p := q[0]
q = q[1:]
t, step := p.first, p.second
if t == end {
return step
}
for i := 0; i < len(t); i++ {
for _, c := range mp[t[i]] {
next := t[:i] + string(c) + t[i+1:]
if s[next] {
q = append(q, pair{next, step + 1})
s[next] = false
}
}
}
}
return -1
}
```

### **...**
Expand Down
130 changes: 129 additions & 1 deletion solution/0400-0499/0433.Minimum Genetic Mutation/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,146 @@

## Solutions

BFS.

<!-- tabs:start -->

### **Python3**

```python

class Solution:
def minMutation(self, start: str, end: str, bank: List[str]) -> int:
s = set(bank)
q = deque([(start, 0)])
mp = {'A': 'TCG', 'T': 'ACG', 'C': 'ATG', 'G': 'ATC'}
while q:
t, step = q.popleft()
if t == end:
return step
for i, v in enumerate(t):
for j in mp[v]:
next = t[:i] + j + t[i + 1:]
if next in s:
q.append((next, step + 1))
s.remove(next)
return -1
```

### **Java**

```java
class Solution {
public int minMutation(String start, String end, String[] bank) {
Set<String> s = new HashSet<>();
for (String b : bank) {
s.add(b);
}
Map<Character, String> mp = new HashMap<>(4);
mp.put('A', "TCG");
mp.put('T', "ACG");
mp.put('C', "ATG");
mp.put('G', "ATC");
Deque<Pair<String, Integer>> q = new LinkedList<>();
q.offer(new Pair<>(start, 0));
while (!q.isEmpty()) {
Pair<String, Integer> p = q.poll();
String t = p.getKey();
int step = p.getValue();
if (end.equals(t)) {
return step;
}
for (int i = 0; i < t.length(); ++i) {
for (char c : mp.get(t.charAt(i)).toCharArray()) {
String next = t.substring(0, i) + c + t.substring(i + 1);
if (s.contains(next)) {
q.offer(new Pair<>(next, step + 1));
s.remove(next);
}
}
}
}
return -1;
}
}
```

### **C++**

```cpp
class Solution {
public:
int minMutation(string start, string end, vector<string>& bank) {
unordered_set<string> s;
for (auto& b : bank) s.insert(b);
unordered_map<char, string> mp;
mp['A'] = "TCG";
mp['T'] = "ACG";
mp['C'] = "ATG";
mp['G'] = "ATC";
queue<pair<string, int>> q;
q.push({start, 0});
while (!q.empty())
{
auto p = q.front();
q.pop();
string t = p.first;
int step = p.second;
if (t == end) return step;
for (int i = 0; i < t.size(); ++i)
{
for (char c : mp[t[i]])
{
string next = t.substr(0, i) + c + t.substr(i + 1, t.size() - i - 1);
if (s.count(next))
{
q.push({next, step + 1});
s.erase(next);
}
}
}
}
return -1;
}
};
```
### **Go**
```go
func minMutation(start string, end string, bank []string) int {
s := make(map[string]bool)
for _, b := range bank {
s[b] = true
}
mp := make(map[byte]string)
mp['A'] = "TCG"
mp['T'] = "ACG"
mp['C'] = "ATG"
mp['G'] = "ATC"
type pair struct {
first string
second int
}
q := []pair{{start, 0}}
for len(q) > 0 {
p := q[0]
q = q[1:]
t, step := p.first, p.second
if t == end {
return step
}
for i := 0; i < len(t); i++ {
for _, c := range mp[t[i]] {
next := t[:i] + string(c) + t[i+1:]
if s[next] {
q = append(q, pair{next, step + 1})
s[next] = false
}
}
}
}
return -1
}
```

### **...**
Expand Down
Loading

0 comments on commit 67886c1

Please sign in to comment.