描述

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. nginx stream 日志设置(Version 1.9.0 +)

    nginx自1.9.0开始提供tcp/udp的反向代理功能,直到1.11.4才开始提供session日志功能. 启用stream日志配置文件 主配置文件/etc/nginx/nginx.conf增加内 ...

  2. 11175-From D to E and Back(思维)

    Problem UVA11175-From D to E and Back Accept: 164  Submit: 607Time Limit: 3000 mSec Problem Descript ...

  3. UVA120-Stacks of Flapjacks(思维)

    Problem UVA120-Stacks of Flapjacks Accept: 9232  Submit: 38455Time Limit: 10000 mSec Problem Descrip ...

  4. [python] 解决pip install download速度过慢问题 更换豆瓣源

    """ python建立pip.ini.py 2016年4月30日 03:35:11 codegay """ import os ini=& ...

  5. python入门学习:5.字典

    python入门学习:5.字典 关键点:字典 5.1 使用字典5.2 遍历字典5.3 嵌套 5.1 使用字典   在python中字典是一系列键-值对.每个键都和一个值关联,你可以使用键来访问与之相关 ...

  6. WPF中修改DataGrid单元格值并保存

    编辑DataGrid中的单元格的内容然后保存是非常常用的功能.主要涉及到的方法就是DataGrid的CellEditEnding  和BeginningEdit .其中BeginningEdit 是当 ...

  7. @ModelAttribute

    在执行Controller方法前都会新建一个Map对象称为隐含模型,该Map对象是共享的,如果一个方法的入参为Map ModelAndMap ModelMap等类型,那么会把隐含模型当做入参赋给方法. ...

  8. Python:Day15 函数

    函数参数补充: 还可以这样传参: def f(*args): print(args) f(*[1,3,4,5]) #输出结果:(1, 3, 4, 5) 注意这是一个元组 def f2(**kwargs ...

  9. 【window】window10永久关闭更新

    在使用pc过程中自己遇到的问题 相关资料:http://www.ghost580.com/win10/2016-10-21/17295.html 作者:smile.轉角 QQ:493177502

  10. web_ui各种元素的操作

    一:元素在最下方,需要拉动滚动条才可显示 1.js中没有xpath定位元素的方法,只有id.tagname,name 2.在python中用这个方法实现 全部显示是这样的 二:针对区域划分,这里有很多 ...