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 ...
随机推荐
- nuxt框架Universal和Spa两种render mode的区别
如下图,官网上对于Universal 和 Spa 两种render mode的区别,并没有加以说明,相信大多数人跟我一样有点懵,不知道选什么好.虽然两个模式创建的项目看不出区别. 先给出推荐选项: U ...
- nginx配置静态webserver
配置静态的web,须要实现一个虚拟主机. step1: 准备工作 1 查看你的网卡地址(我的 192.168.223.135) #ifcon ...
- wpf小技巧——datagrid 滚动条问题
今天在项目中遇到了一个问题,datagrid 不出现滚动条了,拿出来给大家分享下,以作前车之鉴. 很简单的布局代码如下 <Window x:Class="DataGrid_AutoSi ...
- 自定义的库加载不进来,因为库中import的PIL和pillow文件没有pip install
1.自定义的库,加载进来,提示red不能识别这个class或moudle 2.应该展开细节多看下,细节中提示,没有PIL和pillow 3.这个时候在cmd中使用pip安装PIL和pillow pip ...
- k8s 节点的 NodeAffinity 使用
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2 kind: Deployment metadata: name: vi ...
- ESP8266开发综合篇第一节(LUA)-下载和刷固件
本节视频: https://v.youku.com/v_show/id_XNDAwMTI2OTg2MA==.html?spm=a2h3j.8428770.3416059.1 一,整版测试 刷入测试固 ...
- Linux、Windows如何进行性能监控与调优
1.Linux命令行工具 推荐:CentOS 7 1.1 top命令 top命令的输出如下: top命令的输出可以分为两部分:前半部分是系统统计信息,后半部分是进程信息.在统计信息中, 第1行是任务队 ...
- el-date-picker 快捷日期简单计算
const oneDaySeconds = 3600 * 1000 * 24 pickerOptions: { shortcuts: [ { text: '今天', onClick(picker) { ...
- 第一行代码:以太坊(2)-使用Solidity语言开发和测试智能合约
智能合约是以太坊的核心之一,用户可以利用智能合约实现更灵活的代币以及其他DApp.不过在深入讲解如何开发智能合约之前,需要先介绍一下以太坊中用于开发智能合约的Solidity语言,以及相关的开发和测试 ...
- java连接Mysql8
相较于之前版本会有部分改动 pom依赖 <dependency> <groupId>mysql</groupId> <artifactId>mysql- ...