描述

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.

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

class Solution {
public:
/**
* @param start:
* @param end:
* @param bank:
* @return: the minimum number of mutations needed to mutate from "start" to "end"
*/
int minMutation(string &start, string &end, vector<string> &bank) {
// Write your code here
if (bank.empty()) return -1;
vector<char> gens{'A','C','G','T'};
unordered_set<string> s{bank.begin(), bank.end()};
unordered_set<string> visited;
queue<string> q{{start}};
int level = 0;
while (!q.empty()) {
int len = q.size();
for (int i = 0; i < len; ++i) {
string t = q.front(); q.pop();
if (t == end) return level;
for (int j = 0; j < t.size(); ++j) {
char old = t[j];
for (char c : gens) {
t[j] = c;
if (s.count(t) && !visited.count(t)) {
visited.insert(t);
q.push(t);
}
}
t[j] = old;
}
}
++level;
}
return -1;
}
};

1244. Minimum Genetic Mutation的更多相关文章

  1. Leetcode: Minimum Genetic Mutation

    A gene string can be represented by an 8-character long string, with choices from "A", &qu ...

  2. [LeetCode] Minimum Genetic Mutation 最小基因变化

    A gene string can be represented by an 8-character long string, with choices from "A", &qu ...

  3. [Swift]LeetCode433. 最小基因变化 | Minimum Genetic Mutation

    A gene string can be represented by an 8-character long string, with choices from "A", &qu ...

  4. 【LeetCode】433. Minimum Genetic Mutation 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址: https://leetcode. ...

  5. 【leetcode】433. Minimum Genetic Mutation

    题目如下: 解题思路:我的思路很简单,就是利用BFS方法搜索,找到最小值. 代码如下: class Solution(object): def canMutation(self, w, d, c, q ...

  6. [LeetCode] Word Ladder 词语阶梯

    Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...

  7. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  8. 127单词接龙 1· Word Ladder1

    找出最短路径 [抄题]: Given two words (beginWord and endWord), and a dictionary's word list, find the length ...

  9. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

随机推荐

  1. Django-rest-framework 接口实现 版本控制 versioning

    版本控制 rest_framework 提供了 5 种版本控制 以及对应的 写法 url的 更改都可以 在 from rest_framework import versioning 中查看 Acce ...

  2. 两段锁协议(Two-Phase Locking――2PL)

    两段锁协议(Two-Phase Locking――2PL) 两段锁协议规定所有的事务应遵守的规则: ① 在对任何数据进行读.写操作之前,首先要申请并获得对该数据的封锁. ② 在释放一个封锁之后,事务不 ...

  3. UVA12107-Digit Puzzle(迭代加深搜索)

    Problem UVA12107-Digit Puzzle Accept:85  Submit:612 Time Limit: 3000 mSec  Problem Description  Inpu ...

  4. Python:Day05 作业

    购物车: product_list = [['iphone6s',5800],['mac book',9800],['coffee',32],['book',80],['bike',1500]] sh ...

  5. ASM problem : ORA-15001: diskgroup "DGROUP1" does not exist or is not mounted ORA-15040: diskgroup is incomplete

    ============================================================= mos中的详细解释: ODA: After Apply ODA 12.2.1 ...

  6. Springboot实现跨域请求

    之所以需要用到跨域请求,目的在于现在的Java项目,几乎基本上都前后端分离,除一些较老的维护项目外(通常是单体或者是maven多模块形式,不过本质上还是将前端放在webapps下). SpringBo ...

  7. java对象比较

    public class InternDemo { public static void main(String[] args){ /* jdk7版本之后 字符串常量池从Perm Space移到Jav ...

  8. Python脱产8期 Day10 2019/4/24

    一 函数 1.定义:完成 特定 功能的代码块,作为一个整体,对其进行特定的命名,该名字就代表函数>>工具. 2.函数的优点:1.避免代码的冗余:2.让程序结构代码更加清晰:3.让代码更加具 ...

  9. Python中print和return的区别

    有趣的事,Python永远不会缺席! 如需转发,请注明出处:小婷儿的python  https://www.cnblogs.com/xxtalhr/p/10742671.html 一.解释 1.ret ...

  10. vue 热加载问题

    今天是使用vue突然发现没有热加载功能了,然后网上查了一下,配置了一些东西,并没有什么用,然后发现电脑FQ影响 vue 热加载 关掉FQ软件就好了,具体原理我也不清