forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution2.java
35 lines (33 loc) · 1011 Bytes
/
Solution2.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
class Solution {
private int ans;
private Set<String> s;
private static final char[] seq = {'A', 'C', 'G', 'T'};
public int minMutation(String start, String end, String[] bank) {
s = new HashSet<>();
for (String b : bank) {
s.add(b);
}
ans = Integer.MAX_VALUE;
dfs(start, end, 0);
s.remove(start);
return ans == Integer.MAX_VALUE ? -1 : ans;
}
private void dfs(String start, String end, int t) {
if (start.equals(end)) {
ans = Math.min(ans, t);
return;
}
for (int i = 0; i < start.length(); ++i) {
for (char c : seq) {
if (start.charAt(i) == c) {
continue;
}
String nxt = start.substring(0, i) + c + start.substring(i + 1);
if (s.contains(nxt)) {
s.remove(nxt);
dfs(nxt, end, t + 1);
}
}
}
}
}