Minimum Genetic Mutation

题目地址:
https://leetcode.com/problems/minimum-genetic-mutation/description/

题目:
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

解题思路:
这道题就是用map先把图建立起来,然后对这个图进行bfs。

代码:

public class MinimumGeneticMutation {

    /*        enetic Mutations: LC 433     */
    public int minMutation(String start, String end, String[] bank) {
        if(bank == null || bank.length == 0){
            return -1;
        }
        Map<String, Set<String>> map = build(start, end, bank);
        Queue<String> q = new LinkedList<>();
        q.offer(start);
        Set<String> visited = new HashSet<>();
        int count = 0;
        while(q.size() != 0){
            int size = q.size();
            count++;
            for(int i = 0; i <= size - 1; i++){
                String curr = q.poll();
                visited.add(curr);
                Set<String> set = map.get(curr);
                for(String s : set){
                    if(s.equals(end)){
                        return count;
                    }
                    else{
                        if(visited.contains(s)){
                            continue;
                        }
                        else{
                            visited.add(s);
                            q.offer(s);
                        }
                    }
                }
            }
        }
        return -1;
    }

    private Map<String,Set<String>> build(String start, String end, String[] bank) {
        Map<String, Set<String>> map = new HashMap<>();
        map.put(start, new HashSet<>());
        for(String s : bank){
            map.put(s, new HashSet<>());
        }
        for(String key : map.keySet()){
            for(String s : bank){
                if(oneStep(key, s)){
                    map.get(key).add(s);
                }
            }
        }
        return map;
    }

    private boolean oneStep(String key, String s) {
        char[] chars1 = key.toCharArray();
        char[] chars2 = s.toCharArray();
        int diff = 0;
        for(int i = 0; i <= Math.min(chars1.length - 1, chars2.length - 1); i++){
            if(chars1[i] != chars2[i]){
                diff++;
            }
            if(diff > 1){
                return false;
            }
        }
        return diff == 1;
    }

    public static void main(String[] args){
        MinimumGeneticMutation minimumGeneticMutation = new MinimumGeneticMutation();
        String start = "AAAACCCC";
        String end = "CCCCCCCC";
        String[] strings = {"AAAACCCA","AAACCCCA","AACCCCCA","AACCCCCC","ACCCCCCC","CCCCCCCC","AAACCCCC","AACCCCCC"};
        int rst = minimumGeneticMutation.minMutation(start, end, strings);
        System.out.println(rst);
    }
}








Comments

Popular Posts