433. Minimum Genetic Mutation

来源:互联网 发布:淘宝店铺商品搜不到 编辑:程序博客网 时间:2024/06/05 07:51

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
解析:图的广度优先遍历,基因段只包括’A’,’C’,’G’,’T’四个字符,用这四个字符逐一替代基因段中的字符,在基因库中搜索替代后的基因段,如果能找到就把这个新的基因段保存到visited和temp两个Set中,Visited的作用是判断我们是否重复基因段,temp的作用是更新start(初始基因段)一次start基因串被逐一替代有可能出现多个新的在库中存在的基因段所以我们必须要有一个StartSet去存储它,并且当startset.size大于endset时要把两者交换以提升速度

这里写代码片public class Solution {    /**     * @param args     */    public static void main(String[] args) {        // TODO Auto-generated method stub        String start = "AACCGGTT";        String end = "AACCGGTA";        String[] bank = {};        System.out.println(minMutation(start, end, bank));    }    public static int minMutation(String start, String end, String[] bank) {        Set<String> beginSet =new HashSet<String>();        Set<String> endSet = new HashSet<String>();        List<String> bank1 = Arrays.asList(bank);        Character[] cha = {'A','C','G','T'};        List<Character> chas = Arrays.asList(cha);        int len = 0;        HashSet<String> visited = new HashSet<String>();        //String word = "";        beginSet.add(start);        endSet.add(end);        if(!bank1.contains(end))return -1;        while(!beginSet.isEmpty()&&!endSet.isEmpty()){            if(beginSet.size()>endSet.size()){                Set<String> mod = beginSet;                beginSet = endSet;                endSet = mod;            }            Set<String> temp = new HashSet<String>();            for(String word : beginSet){                //String word1 = word;                char[] chs = word.toCharArray();                for(int i = 0;i<chs.length;i++){                    for(char c :chas){                        char old = chs[i];                        chs[i] = c;                        String target = String.valueOf(chs);                        if(endSet.contains(target)){                            return len+1;                        }                        if(!visited.contains(target)&&bank1.contains(target)){                            visited.add(target);                            temp.add(target);                        }                        chs[i] = old;                    }                }            }            beginSet=temp;            len++;        }    return 0;    }}
0 0