Welcome to Subscribe On Youtube
  • import java.util.*;
    
    /**
    
     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:
    
     Starting point is assumed to be valid, so it might not be included in the bank.
     If multiple mutations are needed, all mutations during in the sequence must be valid.
     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
    
     @tag-backtracking
    
     */
    
    public class Minimum_Genetic_Mutation {
    
        // ref: https://leetcode.com/problems/minimum-genetic-mutation/discuss/91484/Java-Solution-using-BFS
        public class Solution {
            public int minMutation(String start, String end, String[] bank) {
    
                if (start.equals(end)) {
                    return 0;
                }
    
                // construct set
                Set<String> bankSet = new HashSet<>();
                bankSet.addAll(Arrays.asList(bank));
    
                char[] charSet = new char[]{'A', 'C', 'G', 'T'};
    
                int level = 0;
                Set<String> visited = new HashSet<>();
                Queue<String> queue = new LinkedList<>();
                queue.offer(start);
                visited.add(start);
    
                while (!queue.isEmpty()) {
                    int size = queue.size(); // get this level count
                    while (size-- > 0) { // @note: this will gurantee go over all this level
                        String curr = queue.poll();
                        if (curr.equals(end)) {
                            return level;
                        }
    
                        char[] currArray = curr.toCharArray();
                        for (int i = 0; i < currArray.length; i++) {
    
                            char old = currArray[i]; // save char in this index
    
                            for (char c : charSet) {
                                currArray[i] = c;
                                String next = new String(currArray);
                                if (!visited.contains(next) && bankSet.contains(next)) {
                                    visited.add(next);
                                    queue.offer(next);
                                }
                            }
    
                            currArray[i] = old; // restore after all above char trials
                        }
                    }
                    level++;
                }
    
                return -1;
            }
        }
    }
    
    ############
    
    class Solution {
        public int minMutation(String start, String end, String[] bank) {
            Set<String> s = new HashSet<>();
            for (String b : bank) {
                s.add(b);
            }
            Map<Character, String> mp = new HashMap<>(4);
            mp.put('A', "TCG");
            mp.put('T', "ACG");
            mp.put('C', "ATG");
            mp.put('G', "ATC");
            Deque<Pair<String, Integer>> q = new LinkedList<>();
            q.offer(new Pair<>(start, 0));
            while (!q.isEmpty()) {
                Pair<String, Integer> p = q.poll();
                String t = p.getKey();
                int step = p.getValue();
                if (end.equals(t)) {
                    return step;
                }
                for (int i = 0; i < t.length(); ++i) {
                    for (char c : mp.get(t.charAt(i)).toCharArray()) {
                        String next = t.substring(0, i) + c + t.substring(i + 1);
                        if (s.contains(next)) {
                            q.offer(new Pair<>(next, step + 1));
                            s.remove(next);
                        }
                    }
                }
            }
            return -1;
        }
    }
    
  • class Solution:
        def minMutation(self, start: str, end: str, bank: List[str]) -> int:
            s = set(bank)
            q = deque([(start, 0)])
            mp = {'A': 'TCG', 'T': 'ACG', 'C': 'ATG', 'G': 'ATC'}
            while q:
                t, step = q.popleft()
                if t == end:
                    return step
                for i, v in enumerate(t):
                    for j in mp[v]:
                        next = t[:i] + j + t[i + 1 :]
                        if next in s:
                            q.append((next, step + 1))
                            s.remove(next)
            return -1
    
    ############
    
    while queue 不空
        cur = queue.pop()
        if cur 有效且未被访问过
            进行处理
        for 节点 in cur 的所有相邻节点
            if 该节点有效
                queue.push(该节点)
    
  • class Solution {
    public:
        int minMutation(string start, string end, vector<string>& bank) {
            unordered_set<string> s;
            for (auto& b : bank) s.insert(b);
            unordered_map<char, string> mp;
            mp['A'] = "TCG";
            mp['T'] = "ACG";
            mp['C'] = "ATG";
            mp['G'] = "ATC";
            queue<pair<string, int>> q;
            q.push({start, 0});
            while (!q.empty()) {
                auto p = q.front();
                q.pop();
                string t = p.first;
                int step = p.second;
                if (t == end) return step;
                for (int i = 0; i < t.size(); ++i) {
                    for (char c : mp[t[i]]) {
                        string next = t.substr(0, i) + c + t.substr(i + 1, t.size() - i - 1);
                        if (s.count(next)) {
                            q.push({next, step + 1});
                            s.erase(next);
                        }
                    }
                }
            }
            return -1;
        }
    };
    
  • func minMutation(start string, end string, bank []string) int {
    	s := make(map[string]bool)
    	for _, b := range bank {
    		s[b] = true
    	}
    	mp := make(map[byte]string)
    	mp['A'] = "TCG"
    	mp['T'] = "ACG"
    	mp['C'] = "ATG"
    	mp['G'] = "ATC"
    	type pair struct {
    		first  string
    		second int
    	}
    	q := []pair{ {start, 0} }
    	for len(q) > 0 {
    		p := q[0]
    		q = q[1:]
    		t, step := p.first, p.second
    		if t == end {
    			return step
    		}
    		for i := 0; i < len(t); i++ {
    			for _, c := range mp[t[i]] {
    				next := t[:i] + string(c) + t[i+1:]
    				if s[next] {
    					q = append(q, pair{next, step + 1})
    					s[next] = false
    				}
    			}
    		}
    	}
    	return -1
    }
    
  • function minMutation(start: string, end: string, bank: string[]): number {
        const queue = [start];
        let res = 0;
        while (queue.length !== 0) {
            const n = queue.length;
            for (let i = 0; i < n; i++) {
                const s1 = queue.shift();
                if (s1 === end) {
                    return res;
                }
    
                for (let j = bank.length - 1; j >= 0; j--) {
                    const s2 = bank[j];
                    let count = 0;
                    for (let k = 0; k < 8; k++) {
                        if (s1[k] !== s2[k]) {
                            count++;
                        }
                    }
                    if (count <= 1) {
                        queue.push(...bank.splice(j, 1));
                    }
                }
            }
            res++;
        }
        return -1;
    }
    
    
  • use std::collections::VecDeque;
    impl Solution {
        pub fn min_mutation(start: String, end: String, mut bank: Vec<String>) -> i32 {
            let mut queue = vec![start].into_iter().collect::<VecDeque<String>>();
            let mut res = 0;
            while !queue.is_empty() {
                let n = queue.len();
                for _ in 0..n {
                    let s1 = queue.pop_front().unwrap();
                    if s1 == end {
                        return res;
                    }
    
                    for i in (0..bank.len()).rev() {
                        let s1 = s1.as_bytes();
                        let s2 = bank[i].as_bytes();
                        let mut count = 0;
                        for j in 0..8 {
                            if s1[j] != s2[j] {
                                count += 1;
                            }
                        }
                        if count <= 1 {
                            queue.push_back(bank.remove(i));
                        }
                    }
                }
                res += 1;
            }
            -1
        }
    }
    
    

All Problems

All Solutions