433. Minimum Genetic Mutation

A gene string can be represented by an 8-character long string, with choices from "A", "C", "G", "T".

Suppose we need to investigate about a mutation (mutation from "start" to "end"), where ONE mutation is defined as ONE single character changed in the gene string.

For example, "AACCGGTT" -> "AACCGGTA" is 1 mutation.

Also, there is a given gene "bank", which records all the valid gene mutations. A gene must be in the bank to make it a valid gene string.

Now, given 3 things - start, end, bank, your task is to determine what is the minimum number of mutations needed to mutate from "start" to "end". If there is no such a mutation, return -1.

Note:

  1. Starting point is assumed to be valid, so it might not be included in the bank.

  2. If multiple mutations are needed, all mutations during in the sequence must be valid.

  3. You may assume start and end string is not the same.

Example 1:

start: "AACCGGTT"
end:   "AACCGGTA"
bank: ["AACCGGTA"]

return: 1

Example 2:

start: "AACCGGTT"
end:   "AAACGGTA"
bank: ["AACCGGTA", "AACCGCTA", "AAACGGTA"]

return: 2

Example 3:

start: "AAAAACCC"
end:   "AACCCCCC"
bank: ["AAAACCCC", "AAACCCCC", "AACCCCCC"]

return: 3
// Single-End BFS
int minMutation(string start, string end, vector<string>& bank) { // time: O(n * str_len); space: O(n * str_len)
    unordered_set<string> genes(bank.begin(), bank.end());
    queue<string> q({start});
    string choices = "ACGT";
    int res = 0;
    while (!q.empty()) {
        for (int k = q.size() - 1; k >= 0; --k) {
            string gene = q.front(); q.pop();
            if (gene == end) return res;
            for (int i = 0; i < gene.length(); ++i) {
                string newGene = gene;
                for (const char& ch : choices) {
                    newGene[i] = ch;
                    if (genes.count(newGene) && newGene != gene) {
                        q.push(newGene);
                        genes.erase(newGene); // avoid visit it twice
                    }
                }
            }
        }
        ++res;
    }
    return -1;
}
// Double-End BFS
int minMutation(string start, string end, vector<string>& bank) { // time: O(n * str_len); space: O(n * str_len)
    unordered_set<string> genes(bank.begin(), bank.end());
    if (!genes.count(end)) return -1;
    unordered_set<string> q1({start}), q2({end});
    string choices = "ACGT";
    int res = 0;
    while (!q1.empty() && !q2.empty()) {
        if (q1.size() > q2.size()) swap(q1, q2);
        unordered_set<string> tmp;
        for (const string& gene : q1) {
            for (int i = 0; i < 8; ++i) {
                string newGene = gene;
                for (const char& ch : choices) {
                    newGene[i] = ch;
                    if (q2.count(newGene)) return res + 1;
                    if (!genes.count(newGene)) continue;
                    genes.erase(newGene); // avoid visiting it twice
                    tmp.insert(newGene);
                }
            }
        }
        ++res;
        swap(q1, tmp);
    }
    return -1;
}
127. Word Ladder

Last updated

Was this helpful?