Skip to content

Commit f5addba

Browse files
committed
feat: add solutions to lc problem: No.0433
No.0433.Minimum Genetic Mutation
1 parent 1ffbeab commit f5addba

File tree

2 files changed

+276
-2
lines changed

2 files changed

+276
-2
lines changed

solution/0400-0499/0433.Minimum Genetic Mutation/README.md

+139-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ bank: ["AAAACCCC", "AAACCCCC", "AACCCCCC"]
6060

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

63-
BFS 找最值。
63+
**方法一:BFS**
64+
65+
**方法二:DFS**
6466

6567
<!-- tabs:start -->
6668

@@ -87,6 +89,28 @@ class Solution:
8789
return -1
8890
```
8991

92+
```python
93+
class Solution:
94+
def minMutation(self, start: str, end: str, bank: List[str]) -> int:
95+
def dfs(start, t):
96+
if start == end:
97+
nonlocal ans
98+
ans = min(ans, t)
99+
return
100+
for i, x in enumerate(start):
101+
for y in 'ACGT':
102+
if x != y:
103+
nxt = start[:i] + y + start[i + 1:]
104+
if nxt in s:
105+
s.remove(nxt)
106+
dfs(nxt, t + 1)
107+
108+
s = set(bank)
109+
ans = float('inf')
110+
dfs(start, 0)
111+
return -1 if ans == float('inf') else ans
112+
```
113+
90114
### **Java**
91115

92116
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -127,6 +151,44 @@ class Solution {
127151
}
128152
```
129153

154+
```java
155+
class Solution {
156+
private int ans;
157+
private Set<String> s;
158+
private static final char[] seq = {'A', 'C', 'G', 'T'};
159+
160+
public int minMutation(String start, String end, String[] bank) {
161+
s = new HashSet<>();
162+
for (String b : bank) {
163+
s.add(b);
164+
}
165+
ans = Integer.MAX_VALUE;
166+
dfs(start, end, 0);
167+
s.remove(start);
168+
return ans == Integer.MAX_VALUE ? -1 : ans;
169+
}
170+
171+
private void dfs(String start, String end, int t) {
172+
if (start.equals(end)) {
173+
ans = Math.min(ans, t);
174+
return;
175+
}
176+
for (int i = 0; i < start.length(); ++i) {
177+
for (char c : seq) {
178+
if (start.charAt(i) == c) {
179+
continue;
180+
}
181+
String nxt = start.substring(0, i) + c + start.substring(i + 1);
182+
if (s.contains(nxt)) {
183+
s.remove(nxt);
184+
dfs(nxt, end, t + 1);
185+
}
186+
}
187+
}
188+
}
189+
}
190+
```
191+
130192
### **C++**
131193

132194
```cpp
@@ -167,6 +229,44 @@ public:
167229
};
168230
```
169231
232+
```cpp
233+
class Solution {
234+
public:
235+
int ans;
236+
string seq = "ACGT";
237+
238+
int minMutation(string start, string end, vector<string>& bank) {
239+
unordered_set<string> s;
240+
for (auto& b : bank) s.insert(b);
241+
ans = INT_MAX;
242+
s.erase(start);
243+
dfs(start, end, s, 0);
244+
return ans == INT_MAX ? -1 : ans;
245+
}
246+
247+
void dfs(string& start, string& end, unordered_set<string>& s, int t) {
248+
if (start == end)
249+
{
250+
ans = min(ans, t);
251+
return;
252+
}
253+
for (int i = 0; i < start.size(); ++i)
254+
{
255+
for (char& c : seq)
256+
{
257+
if (start[i] == c) continue;
258+
string nxt = start.substr(0, i) + c + start.substr(i + 1, start.size() - i - 1);
259+
if (s.count(nxt))
260+
{
261+
s.erase(nxt);
262+
dfs(nxt, end, s, t + 1);
263+
}
264+
}
265+
}
266+
}
267+
};
268+
```
269+
170270
### **Go**
171271

172272
```go
@@ -206,6 +306,44 @@ func minMutation(start string, end string, bank []string) int {
206306
}
207307
```
208308

309+
```go
310+
func minMutation(start string, end string, bank []string) int {
311+
s := make(map[string]bool)
312+
for _, b := range bank {
313+
s[b] = true
314+
}
315+
ans := math.MaxInt32
316+
s[start] = false
317+
seq := []rune{'A', 'C', 'G', 'T'}
318+
var dfs func(start string, t int)
319+
dfs = func(start string, t int) {
320+
if start == end {
321+
if ans > t {
322+
ans = t
323+
}
324+
return
325+
}
326+
for i, x := range start {
327+
for _, y := range seq {
328+
if x == y {
329+
continue
330+
}
331+
nxt := start[:i] + string(y) + start[i+1:]
332+
if s[nxt] {
333+
s[nxt] = false
334+
dfs(nxt, t+1)
335+
}
336+
}
337+
}
338+
}
339+
dfs(start, 0)
340+
if ans == math.MaxInt32 {
341+
return -1
342+
}
343+
return ans
344+
}
345+
```
346+
209347
### **...**
210348

211349
```

solution/0400-0499/0433.Minimum Genetic Mutation/README_EN.md

+137-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353

5454
## Solutions
5555

56-
BFS.
56+
BFS or DFS.
5757

5858
<!-- tabs:start -->
5959

@@ -78,6 +78,28 @@ class Solution:
7878
return -1
7979
```
8080

81+
```python
82+
class Solution:
83+
def minMutation(self, start: str, end: str, bank: List[str]) -> int:
84+
def dfs(start, t):
85+
if start == end:
86+
nonlocal ans
87+
ans = min(ans, t)
88+
return
89+
for i, x in enumerate(start):
90+
for y in 'ACGT':
91+
if x != y:
92+
nxt = start[:i] + y + start[i + 1:]
93+
if nxt in s:
94+
s.remove(nxt)
95+
dfs(nxt, t + 1)
96+
97+
s = set(bank)
98+
ans = float('inf')
99+
dfs(start, 0)
100+
return -1 if ans == float('inf') else ans
101+
```
102+
81103
### **Java**
82104

83105
```java
@@ -116,6 +138,44 @@ class Solution {
116138
}
117139
```
118140

141+
```java
142+
class Solution {
143+
private int ans;
144+
private Set<String> s;
145+
private static final char[] seq = {'A', 'C', 'G', 'T'};
146+
147+
public int minMutation(String start, String end, String[] bank) {
148+
s = new HashSet<>();
149+
for (String b : bank) {
150+
s.add(b);
151+
}
152+
ans = Integer.MAX_VALUE;
153+
dfs(start, end, 0);
154+
s.remove(start);
155+
return ans == Integer.MAX_VALUE ? -1 : ans;
156+
}
157+
158+
private void dfs(String start, String end, int t) {
159+
if (start.equals(end)) {
160+
ans = Math.min(ans, t);
161+
return;
162+
}
163+
for (int i = 0; i < start.length(); ++i) {
164+
for (char c : seq) {
165+
if (start.charAt(i) == c) {
166+
continue;
167+
}
168+
String nxt = start.substring(0, i) + c + start.substring(i + 1);
169+
if (s.contains(nxt)) {
170+
s.remove(nxt);
171+
dfs(nxt, end, t + 1);
172+
}
173+
}
174+
}
175+
}
176+
}
177+
```
178+
119179
### **C++**
120180

121181
```cpp
@@ -156,6 +216,44 @@ public:
156216
};
157217
```
158218
219+
```cpp
220+
class Solution {
221+
public:
222+
int ans;
223+
string seq = "ACGT";
224+
225+
int minMutation(string start, string end, vector<string>& bank) {
226+
unordered_set<string> s;
227+
for (auto& b : bank) s.insert(b);
228+
ans = INT_MAX;
229+
s.erase(start);
230+
dfs(start, end, s, 0);
231+
return ans == INT_MAX ? -1 : ans;
232+
}
233+
234+
void dfs(string& start, string& end, unordered_set<string>& s, int t) {
235+
if (start == end)
236+
{
237+
ans = min(ans, t);
238+
return;
239+
}
240+
for (int i = 0; i < start.size(); ++i)
241+
{
242+
for (char& c : seq)
243+
{
244+
if (start[i] == c) continue;
245+
string nxt = start.substr(0, i) + c + start.substr(i + 1, start.size() - i - 1);
246+
if (s.count(nxt))
247+
{
248+
s.erase(nxt);
249+
dfs(nxt, end, s, t + 1);
250+
}
251+
}
252+
}
253+
}
254+
};
255+
```
256+
159257
### **Go**
160258

161259
```go
@@ -195,6 +293,44 @@ func minMutation(start string, end string, bank []string) int {
195293
}
196294
```
197295

296+
```go
297+
func minMutation(start string, end string, bank []string) int {
298+
s := make(map[string]bool)
299+
for _, b := range bank {
300+
s[b] = true
301+
}
302+
ans := math.MaxInt32
303+
s[start] = false
304+
seq := []rune{'A', 'C', 'G', 'T'}
305+
var dfs func(start string, t int)
306+
dfs = func(start string, t int) {
307+
if start == end {
308+
if ans > t {
309+
ans = t
310+
}
311+
return
312+
}
313+
for i, x := range start {
314+
for _, y := range seq {
315+
if x == y {
316+
continue
317+
}
318+
nxt := start[:i] + string(y) + start[i+1:]
319+
if s[nxt] {
320+
s[nxt] = false
321+
dfs(nxt, t+1)
322+
}
323+
}
324+
}
325+
}
326+
dfs(start, 0)
327+
if ans == math.MaxInt32 {
328+
return -1
329+
}
330+
return ans
331+
}
332+
```
333+
198334
### **...**
199335

200336
```

0 commit comments

Comments
 (0)