class Solution {
public:
int minMutation(string startGene, string endGene, vector& bank) {
if (startGene == endGene) {
return 0;
}
std::unordered_set bank_map;
for (auto & s : bank) {
bank_map.emplace(s);
}
if (0 == bank_map.count(endGene)) {
return -1;
}
std::unordered_set visited;
char keys[4] = {'A', 'C', 'G', 'T'};
std::queue qu;
qu.emplace(startGene);
visited.emplace(startGene);
int step = 1;
while (!qu.empty()) {
int sz = qu.size();
for (int i = 0; i < sz; ++i) {
std::string cur = qu.front();
qu.pop();
for (int j = 0; j < 8; ++j) {
for (int k = 0; k < 4; ++k) {
std::string next = cur;
next[j] = keys[k];
if (visited.count(next) == 0 && bank_map.count(next)) {
if (next == endGene) {
return step;
}
qu.emplace(next);
visited.emplace(next);
}
}
}
}
step++;
}
return -1;
}
}; 上一篇:Qt隐式共享浅析