-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimumGeneticMutation.cpp
More file actions
39 lines (36 loc) · 1.07 KB
/
minimumGeneticMutation.cpp
File metadata and controls
39 lines (36 loc) · 1.07 KB
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
// Source: https://leetcode.com/problems/minimum-genetic-mutation/
// Author: Miao Zhang
// Date: 2021-02-11
class Solution {
public:
int minMutation(string start, string end, vector<string>& bank) {
queue<string> q;
q.push(start);
unordered_set<string> visited;
visited.insert(start);
int mutation = 0;
while (!q.empty()) {
int n = q.size();
for (int i = 0; i < n; i++) {
string cur = std::move(q.front());
q.pop();
if (cur == end) return mutation;
for (string & gene: bank) {
if (visited.count(gene) || !validMutation(cur, gene)) continue;
visited.insert(gene);
q.push(gene);
}
}
mutation++;
}
return -1;
}
private:
bool validMutation(string& s1, string& s2) {
int cnt = 0;
for (int i = 0; i < s1.length(); i++) {
if (s1[i] != s2[i] && cnt++) return false;
}
return true;
}
};