/
MinimumGeneticMutation.java
74 lines (60 loc) · 1.88 KB
/
MinimumGeneticMutation.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// https://leetcode.com/problems/minimum-genetic-mutation
class Solution {
public int minMutation(String start, String end, String[] bank) {
Set<String> mutations = new HashSet<>();
mutations.add(start);
if (start.equals(end)) {
return 0;
}
for (String mutation: bank) {
mutations.add(mutation);
}
Map<String, Set<String>> graph = new HashMap<>();
for (String x: mutations) {
for (String y: mutations) {
if (!x.equals(y)) {
int dist = distance(x, y);
if (dist == 1) {
graph.computeIfAbsent(x, i -> new HashSet<>()).add(y);
}
}
}
}
Deque<Level> queue = new ArrayDeque<>();
queue.addFirst(new Level(start, 0));
Set<String> visited = new HashSet<>();
while (!queue.isEmpty()) {
Level l = queue.pollFirst();
visited.add(l.s);
Set<String> set = graph.get(l.s);
if (set == null) {
break;
}
if (set.contains(end)) {
return l.level + 1;
}
for (String m: set) {
if (!visited.contains(m))
queue.add(new Level(m, l.level + 1));
}
}
return -1;
}
int distance(String a, String b) {
int distance = 0;
for (int i = 0; i < 8; i++) {
if (a.charAt(i) != b.charAt(i)) {
distance++;
}
}
return distance;
}
class Level {
String s;
int level;
Level(String s, int level) {
this.s = s;
this.level = level;
}
}
}