(Java) LeetCode 433. Minimum Genetic Mutation —— 最小基因變化

來源:互聯網
上載者:User

標籤:art   利用   aaa   from   gen   har   tip   廣度優先搜尋   move   

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

 

本題和LeetCode 127. Word Ladder —— 單詞接龍完全一樣,沒有任何區別,利用廣度優先搜尋尋找最短路徑。欣慰自己寫代碼一遍過,直接AC。

 

Java

class Solution {    public int minMutation(String start, String end, String[] bank) {        if (bank == null || bank.length == 0) return -1;        char[] gen = {‘A‘,‘C‘,‘G‘,‘T‘};        HashSet<String> bankSet = new HashSet<>();        for (String s : bank)            bankSet.add(s);        Queue<String> q = new LinkedList<>();        HashMap<String, Integer> res = new HashMap<>();        res.put(start, 0);        q.add(start);        while (!q.isEmpty()) {            String s = q.poll();            bankSet.remove(s);            for (int i = 0; i < s.length(); i++) {                char[] next = s.toCharArray();                for (char c : gen) {                    next[i] = c;                    String nextS = new String(next);                    if (bankSet.contains(nextS)) {                        res.put(nextS, res.get(s) + 1);                        if (nextS.equals(end)) return res.get(nextS);                        q.add(nextS);                    }                }            }        }        return -1;    }}

 

(Java) LeetCode 433. Minimum Genetic Mutation —— 最小基因變化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.