描述

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

  1. class Solution {
  2. public:
  3. /**
  4. * @param start:
  5. * @param end:
  6. * @param bank:
  7. * @return: the minimum number of mutations needed to mutate from "start" to "end"
  8. */
  9. int minMutation(string &start, string &end, vector<string> &bank) {
  10. // Write your code here
  11. if (bank.empty()) return -1;
  12. vector<char> gens{'A','C','G','T'};
  13. unordered_set<string> s{bank.begin(), bank.end()};
  14. unordered_set<string> visited;
  15. queue<string> q{{start}};
  16. int level = 0;
  17. while (!q.empty()) {
  18. int len = q.size();
  19. for (int i = 0; i < len; ++i) {
  20. string t = q.front(); q.pop();
  21. if (t == end) return level;
  22. for (int j = 0; j < t.size(); ++j) {
  23. char old = t[j];
  24. for (char c : gens) {
  25. t[j] = c;
  26. if (s.count(t) && !visited.count(t)) {
  27. visited.insert(t);
  28. q.push(t);
  29. }
  30. }
  31. t[j] = old;
  32. }
  33. }
  34. ++level;
  35. }
  36. return -1;
  37. }
  38. };

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. JSP内置对象概述

    JSP内置对象预先定义了九个这个的对象: request(请求) . response (响应). session (会话). application (应用程序). out . pageContex ...

  2. 前端性能优化成神之路-HTTP压缩开启gzip

    什么是HTTP压缩 HTTP压缩是指: Web服务器和浏览器之间压缩传输的”文本内容“的方法. HTTP采用通用的压缩算法,比如gzip来压缩HTML,Javascript, CSS文件. 能大大减少 ...

  3. SQL IN 操作符

    IN 操作符 IN 操作符允许我们在 WHERE 子句中规定多个值. SQL IN 语法 SELECT column_name(s) FROM table_name WHERE column_name ...

  4. UVA11491-Erasing ans Winning(贪心)

    Problem UVA11491-Erasing ans Winning Accept: 799  Submit: 5753Time Limit: 3000 mSec Problem Descript ...

  5. 需要熟练的Python知识点

    数据与列表元祖字符串 repr(x) 将对象x转换为表达式字符串 eval(str) 用来计算在字符串中的有效Python表达式,并返回一个对象 unichr(x) 将一个整数转换为Unicode字符 ...

  6. 最长回文(manacher模板)

    #include<iostream> #include<algorithm> #include<cstring> #include<cstdio> us ...

  7. oracle11g处理空表

    先查询一下当前用户下的所有空表select table_name from user_tables where NUM_ROWS=0;用以下这句查找空表select 'alter table '||t ...

  8. 吴恩达课后作业学习2-week2-优化算法

    参考:https://blog.csdn.net/u013733326/article/details/79907419 希望大家直接到上面的网址去查看代码,下面是本人的笔记 我们需要做以下几件事:  ...

  9. jenkins使用1----初始化设置

    ####一.基本设置 1.首先找到系统管理 2.再找到全局配置一把黄色的锁头 3.新增JDK.Maven等 别名随便 下面的值添加jdk在jenkins这台机器上的位置,如果没找到可以点击自动安装,并 ...

  10. ogg BR – BOUNDED RECOVERY 测试案例

    首先,我们来看两个OGG同步中可能的问题: l oracle在线日志包含已提交的和未提交的事务,但OGG只会将已提交的事务写入到队列文件.因此,针对未提交的事务,特别是未提交的长事务,OGG会怎样处理 ...