@@ -6,10 +6,10 @@ public int minMutation(String start, String end, String[] bank) {
66 char [] GENES = new char []{'A' ,'C' ,'G' ,'T' };
77 Set <String > banks = new HashSet <String >();
88 for (String b : bank ) banks .add (b );
9+ if (!banks .contains (end )) return -1 ;
910 banks .add (start );
1011
11- Set <String > visited = new HashSet ();
12- DFS (banks , visited , start , 0 , GENES , end );
12+ DFS (banks , new HashSet (), start , 0 , GENES , end );
1313
1414 return ans == Integer .MAX_VALUE ? -1 : ans ;
1515 }
@@ -26,13 +26,14 @@ private void DFS(Set<String> banks, Set<String> visited, String g, int mutations
2626 char [] chars = g .toCharArray ();
2727
2828 for (int i =0 ;i <chars .length ;i ++) {
29+ char oldChar = chars [i ];
2930 for (int j =0 ; j < GENES .length ; j ++) {
30- char oldChar = chars [ i ] ;
31+ if ( oldChar == GENES [ j ]) continue ;
3132 chars [i ] = GENES [j ];
3233 String newGene = new String (chars );
3334 DFS (banks , visited , newGene , mutations +1 , GENES , end );
34- chars [i ] = oldChar ;
3535 }
36+ chars [i ] = oldChar ;
3637 }
3738 }
3839}
0 commit comments