-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathMinimum Genetic Mutation.cpp
37 lines (31 loc) · 1.08 KB
/
Minimum Genetic Mutation.cpp
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
class Solution {
public:
int minMutation(string start, string end, vector<string>& bank) {
unordered_set<string> st(bank.begin(), bank.end());
if (st.find(end) == st.end()) return -1;
queue<string> q;
q.push(start);
unordered_set<string> vis;
vis.insert(start);
int res=0;
while (not q.empty()) {
int sz = q.size();
while (sz--) {
string currString = q.front(); q.pop();
if (currString == end) return res;
for (int i=0;i<currString.size();i++) {
string temp = currString;
for (auto g: string("ATGC")) {
temp[i] = g;
if (st.find(temp) != st.end() and (vis.find(temp) == vis.end())) {
q.push(temp);
vis.insert(temp);
}
}
}
}
res++;
}
return -1;
}
};