-
Notifications
You must be signed in to change notification settings - Fork 1
/
433.cpp
executable file
·39 lines (38 loc) · 1.11 KB
/
433.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
38
39
class Solution {
public:
unordered_map<string,int> validGenes;
int res;
vector<char> changes{'A','C','G','T'};
void dfs(string cur, string end, unordered_set<int> reach, int step){
if (cur == end){
if (res != -1){
res = min(res,step);
}
else{
res = step;
}
return;
}
for (int i = 0; i < cur.length(); i++){
for (auto change : changes){
string future = cur;
future[i] = change;
if (validGenes.count(future) && !reach.count(validGenes[future])){
reach.insert(validGenes[future]);
dfs(future, end, reach, step + 1);
reach.erase(validGenes[future]);
}
}
}
}
int minMutation(string start, string end, vector<string>& bank) {
int index = 0;
for (auto gene: bank){
validGenes[gene] = index++;
}
res = -1;
unordered_set<int> reach;
dfs(start,end,reach, 0);
return res;
}
};