433 Minimum Genetic Mutation

1. Question

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:
  1. 1.
    Starting point is assumed to be valid, so it might not be included in the bank.
  2. 2.
    If multiple mutations are needed, all mutations during in the sequence must be valid.
  3. 3.
    You may assume start and end string is not the same.
Example 1:
1
start: "AACCGGTT"
2
end: "AACCGGTA"
3
bank: ["AACCGGTA"]
4
5
return: 1
Copied!
Example 2:
1
start: "AACCGGTT"
2
end: "AAACGGTA"
3
bank: ["AACCGGTA", "AACCGCTA", "AAACGGTA"]
4
5
return: 2
Copied!
Example 3:
1
start: "AAAAACCC"
2
end: "AACCCCCC"
3
bank: ["AAAACCCC", "AAACCCCC", "AACCCCCC"]
4
5
return: 3
Copied!

2. Implementation

(1) BFS
1
class Solution {
2
public int minMutation(String start, String end, String[] bank) {
3
Set<String> sequences = new HashSet<>();
4
5
char[] code = {'A', 'C', 'G', 'T'};
6
7
for (String seq : bank) {
8
sequences.add(seq);
9
}
10
11
int level = 0;
12
13
Set<String> visited = new HashSet<>();
14
Queue<String> queue = new LinkedList<>();
15
queue.add(start);
16
17
while (!queue.isEmpty()) {
18
int size = queue.size();
19
20
for (int i = 0; i < size; i++) {
21
String curGene = queue.remove();
22
23
if (curGene.equals(end)) {
24
return level;
25
}
26
27
char[] letters = curGene.toCharArray();
28
29
for (int j = 0; j < letters.length; j++) {
30
char oldC = letters[j];
31
for (char c : code) {
32
letters[j] = c;
33
34
String nextGene = new String(letters);
35
36
if (sequences.contains(nextGene) && !visited.contains(nextGene)) {
37
visited.add(nextGene);
38
queue.add(nextGene);
39
}
40
}
41
letters[j] = oldC;
42
}
43
}
44
++level;
45
}
46
return -1;
47
}
48
}
Copied!
(2) Bi-directional BFS
1
class Solution {
2
public int minMutation(String start, String end, String[] bank) {
3
if (start.length() != end.length()) {
4
return -1;
5
}
6
7
Set<String> sequences = new HashSet<>();
8
9
char[] code = {'A', 'C', 'G', 'T'};
10
11
for (String seq : bank) {
12
sequences.add(seq);
13
}
14
15
if (!sequences.contains(end)) {
16
return -1;
17
}
18
19
int level = 0;
20
Set<String> visited = new HashSet<>();
21
Set<String> beginSet = new HashSet<>();
22
Set<String> endSet = new HashSet<>();
23
24
beginSet.add(start);
25
endSet.add(end);
26
27
Set<String> temp = null;
28
29
while (!beginSet.isEmpty() && !endSet.isEmpty()) {
30
System.out.println("beginSet size: " + beginSet.size() + " endSet size: " + endSet.size());
31
if (beginSet.size() >= endSet.size()) {
32
temp = beginSet;
33
beginSet = endSet;
34
endSet = temp;
35
}
36
37
temp = new HashSet<>();
38
39
for (String gene : beginSet) {
40
char[] letters = gene.toCharArray();
41
42
for (int i = 0; i < letters.length; i++) {
43
char oldC = letters[i];
44
45
for (char c : code) {
46
if (c == oldC) continue;
47
letters[i] = c;
48
49
String nextGene = new String(letters);
50
51
if (endSet.contains(nextGene)) {
52
return level + 1;
53
}
54
55
if (sequences.contains(nextGene) && !visited.contains(nextGene)) {
56
visited.add(nextGene);
57
temp.add(nextGene);
58
}
59
}
60
letters[i] = oldC;
61
}
62
}
63
beginSet = temp;
64
++level;
65
}
66
return -1;
67
}
68
}
Copied!

3. Time & Space Complexity

BFS: 时间复杂度O(4* L * m) => O(mL), L为start的长度,m为bank的size, 空间复杂度O(m)