1244. Minimum Genetic Mutation
描述
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的更多相关文章
- Leetcode: Minimum Genetic Mutation
A gene string can be represented by an 8-character long string, with choices from "A", &qu ...
- [LeetCode] Minimum Genetic Mutation 最小基因变化
A gene string can be represented by an 8-character long string, with choices from "A", &qu ...
- [Swift]LeetCode433. 最小基因变化 | Minimum Genetic Mutation
A gene string can be represented by an 8-character long string, with choices from "A", &qu ...
- 【LeetCode】433. Minimum Genetic Mutation 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址: https://leetcode. ...
- 【leetcode】433. Minimum Genetic Mutation
题目如下: 解题思路:我的思路很简单,就是利用BFS方法搜索,找到最小值. 代码如下: class Solution(object): def canMutation(self, w, d, c, q ...
- [LeetCode] Word Ladder 词语阶梯
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- 127单词接龙 1· Word Ladder1
找出最短路径 [抄题]: Given two words (beginWord and endWord), and a dictionary's word list, find the length ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
随机推荐
- 单页应用 - Token 验证
单页应用 - Token 验证 转:https://juejin.im/post/58da720b570c350058ecd40f 第一次接触单页应用,记录公司项目关于Token验证知识. Token ...
- WIN10中DOCKER的安装
1.docker的安装 环境准备 Docker for Windows是一个Docker Community Edition(CE)应用程序.Docker for Windows安装包包含了在Wind ...
- C#从IE缓存读取图片
如果用HttpWebRequest和HttpWebResponse从服务器取图片,会触发图片变化.于是想到从IE缓存读取图片 参考https://www.cnblogs.com/yelaiju/arc ...
- masm的调试命令(debug)
-u命令:查看汇编代码: -t命令:执行下一条语句 -g + 的内存:跳转到该内存所对应的语句(再用t命令执行该条命令) -r命令:查看寄存器的内容(后可直接接寄存器的名称,就只查看该寄存器的内容) ...
- IDEA+Maven+web项目
用IDEA创建一个简单的Maven web项目:https://blog.csdn.net/sinat_34596644/article/details/52891274
- FineUIMvc随笔(2)怎样在控件中嵌套 HTML
声明:FineUIMvc(基础版)是免费软件,本系列文章适用于基础版. 用户需求 有网友在<FineUI总群1>问这么一个问题:怎么把 HTML 嵌套在控件中? 这是很多刚学习 FineU ...
- PowerMock单元测试踩坑与总结
1.Mock是什么? 通过提供定制的类加载器以及一些字节码篡改技巧的应用,PowerMock 现了对静态方法.构造方法.私有方法以及 Final 方法的模拟支持,对静态初始化过程的移除等强大的功能. ...
- orleans发送广播消息
一个client发送消息给orleans, 就只需要掉用Grain的函数就行了. 但是有时候Grain需要发送消息给client, 在orleans里面, 就只能通过Observer来实现. publ ...
- python 可调用对象之类实例
可调用对象,即任何可以通过函数操作符()来调用的对象. python可调用对象大致可以分为4类: 1.函数 python中有三种函数:内建函数(BIFs).用户自定义函数(UDF).lambda表达式 ...
- Jenkins - Extended E-mail配置教程
前言: 在Jenkins的使用中邮件提醒是一个常用功能,Extended E-mail Notification是一个功能更为齐全,使用也更为复杂的插件,本文即将为大家详细讲解如何配置相关内容,感兴趣 ...