-
-
Notifications
You must be signed in to change notification settings - Fork 9k
/
Copy pathSolution.rs
32 lines (30 loc) · 973 Bytes
/
Solution.rs
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
use std::collections::{HashSet, VecDeque};
impl Solution {
pub fn min_mutation(start_gene: String, end_gene: String, bank: Vec<String>) -> i32 {
let mut q = VecDeque::new();
q.push_back((start_gene.clone(), 0));
let mut vis = HashSet::new();
vis.insert(start_gene);
while let Some((gene, depth)) = q.pop_front() {
if gene == end_gene {
return depth;
}
for next in &bank {
let mut c = 2;
for k in 0..8 {
if gene.as_bytes()[k] != next.as_bytes()[k] {
c -= 1;
}
if c == 0 {
break;
}
}
if c > 0 && !vis.contains(next) {
vis.insert(next.clone());
q.push_back((next.clone(), depth + 1));
}
}
}
-1
}
}