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.

Note:

  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

这道题跟之前的 Word Ladder 完全是一道题啊,换个故事就直接来啊,越来不走心了啊。不过博主做的时候并没有想起来是之前一样的题,而是先按照脑海里第一个浮现出的思路做的,发现也通过OJ了。博主使用的一种BFS的搜索,先建立bank数组的距离场,这里距离就是两个字符串之间不同字符的个数。然后以start字符串为起点,向周围距离为1的点扩散,采用BFS搜索,每扩散一层,level自加1,当扩散到end字符串时,返回当前level即可。注意我们要把start字符串也加入bank中,而且此时我们也知道start的坐标位置,bank的最后一个位置,然后在建立距离场的时候,调用一个count子函数,用来统计输入的两个字符串之间不同字符的个数,注意dist[i][j]和dist[j][i]是相同,所以我们只用算一次就行了。然后我们进行BFS搜索,用一个visited集合来保存遍历过的字符串,注意检测距离的时候,dist[i][j]和dist[j][i]只要有一个是1,就可以了,参见代码如下:

解法一:

class Solution {
public:
int minMutation(string start, string end, vector<string>& bank) {
if (bank.empty()) return -;
bank.push_back(start);
int res = , n = bank.size();
queue<int> q{{n - }};
unordered_set<int> visited;
vector<vector<int>> dist(n, vector<int>(n, ));
for (int i = ; i < n; ++i) {
for (int j = i + ; j < n; ++j) {
dist[i][j] = count(bank[i], bank[j]);
}
}
while (!q.empty()) {
++res;
for (int i = q.size(); i > ; --i) {
int t = q.front(); q.pop();
visited.insert(t);
for (int j = ; j < n; ++j) {
if ((dist[t][j] != && dist[j][t] != ) || visited.count(j)) continue;
if (bank[j] == end) return res;
q.push(j);
}
}
}
return -;
}
int count(string word1, string word2) {
int cnt = , n = word1.size();
for (int i = ; i < n; ++i) {
if (word1[i] != word2[i]) ++cnt;
}
return cnt;
}
};

下面这种解法跟之前的那道 Word Ladder 是一样的,也是用的BFS搜索。跟上面的解法不同之处在于,对于遍历到的字符串,我们不再有距离场,而是对于每个字符,我们都尝试将其换为一个新的字符,每次只换一个,这样会得到一个新的字符串,如果这个字符串在bank中存在,说明这样变换是合法的,加入visited集合和queue中等待下一次遍历,记得在下次置换字符的时候要将之前的还原。我们在queue中取字符串出来遍历的时候,先检测其是否和end相等,相等的话返回level,参见代码如下:

解法二:

class Solution {
public:
int minMutation(string start, string end, vector<string>& bank) {
if (bank.empty()) return -;
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 = ;
while (!q.empty()) {
for (int i = q.size(); i > ; --i) {
string t = q.front(); q.pop();
if (t == end) return level;
for (int j = ; 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 -;
}
};

再来看一种递归的解法,跟 Permutations 中的解法一有些类似,是遍历bank中的字符串,跟当前的字符串cur相比较,调用isDiffOne()函数判断,若正好跟cur相差一个字符,并且之前没有访问过,那么先在visited数组中标记为true,然后调用递归函数,若返回的不为-1,则用其更新结果res,因为-1代表无法变换成cur。调用完递归后恢复状态,在visited数组中标记为false。循环结束后,看res的值,若还是n+1,表示无法更新,返回-1,否则返回res+1,因为这里的res是变换了一次后到达目标字符串的最小变化次数,所以要加上当前的这次变换。至于isDiffOne()函数就没啥难度了,就是一个一个的比较,不同就累加计数器cnt,参见代码如下:

解法三:

class Solution {
public:
int minMutation(string start, string end, vector<string>& bank) {
if (bank.empty()) return -;
vector<bool> visited(bank.size(), false);
return helper(start, end, bank, visited);
}
int helper(string cur, string end, vector<string>& bank, vector<bool>& visited) {
if (cur == end) return ;
int n = bank.size(), res = n + ;
for (int i = ; i < n; ++i) {
if (visited[i] || !isDiffOne(bank[i], cur)) continue;
visited[i] = true;
int t = helper(bank[i], end, bank, visited);
if (t != -) res = min(res, t);
visited[i] = false;
}
return res == n + ? - : res + ;
}
bool isDiffOne(string& s1, string& s2) {
int cnt = , n = s1.size();
for (int i = ; i < n; ++i) {
if (s1[i] != s2[i]) ++cnt;
if (cnt > ) break;
}
return cnt == ;
}
};

类似题目:

Word Ladder

Word Ladder II

Permutations

参考资料:

https://leetcode.com/problems/minimum-genetic-mutation/

https://leetcode.com/problems/minimum-genetic-mutation/discuss/91491/dfs-java

https://leetcode.com/problems/minimum-genetic-mutation/discuss/91484/java-solution-using-bfs

 

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 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 433.最小基因变化

    最小基因变化 一条基因序列由一个带有8个字符的字符串表示,其中每个字符都属于 "A", "C", "G", "T"中的任 ...

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

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

  4. Java实现 LeetCode 433 最小基因变化

    433. 一条基因序列由一个带有8个字符的字符串表示,其中每个字符都属于 "A", "C", "G", "T"中的任意一 ...

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

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

  6. 【leetcode】433. Minimum Genetic Mutation

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

  7. [LeetCode] Minimum Height Trees 最小高度树

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

  8. [LeetCode] Minimum Window Substring 最小窗口子串

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  9. [LeetCode] Minimum Path Sum 最小路径和

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

随机推荐

  1. Python中的classmethod与staticmethod

    首先,这是一个经典的问题. 我们首先做一个比较: classmethod的第一个参数是cls,即调用的时候要把类传入 这意味着我们我们可以在classmethod里使用类的属性,而不是类的实例的属性( ...

  2. 阿尔法冲刺——Postmortem会议

    设想与目标 1.我们软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 这个问题,我们觉得我们的软件目标还是比较明确的,在SRS中也给出了典型用户和典型场景的清晰的描述. 2 ...

  3. Alpha冲刺No.6

    站立式会议 继续页面设计 在安卓内构件数据库相应类 解决摄像头.照片的使用的异常问题 二.实际项目进展 页面设计完成百分80 类架构完成 在虚拟机中,能够完成摄像头的调用和程序的使用 三.燃尽图 四. ...

  4. 每日冲刺报告--Day2

    敏捷冲刺每日报告--Day2 情况简介 今天我们三个人在一起开了会,分析了我们面临的情况以及下一阶段的计划.一个重大的改进是,我们准备把之前用txt文件格式存储订阅列表改成了文件json格式. 任务进 ...

  5. 算法第四版学习笔记之优先队列--Priority Queues

    软件:DrJava 参考书:算法(第四版) 章节:2.4优先队列(以下截图是算法配套视频所讲内容截图) 1:API 与初级实现 2:堆得定义 3:堆排序 4:事件驱动的仿真 优先队列最重要的操作就是删 ...

  6. 自主学习之RxSwift(一) -----Driver

    对于RxSwift,我也是初学者,此系列来记录我学习RxSwift的历程! (一) 想必关于Drive大家一定在RxSwift的Demo中看到过,也一定有些不解,抱着一起学习的态度,来了解一下Driv ...

  7. Java 多线程 从无到有

    个人总结:望对屏幕对面的您有所帮助 一. 线程概述 进程: 有独立的内存控件和系统资源 应用程序的执行实例 启动当前电脑任务管理器:taskmgr 进程是程序(任务)的执行过程,它持有资源(共享内存, ...

  8. css3动画transition详解2

    transition主要包含四个属性值:执行变换的属性:transition-property,变换延续的时间:transition-duration,在延续时间段,变换的速率变化transition ...

  9. 【非官方】Surging 微服务框架使用入门

    前言 本文非 Surging 官方教程,只是自己学习的总结.如有哪里不对,还望指正. 我对 surging 的看法 我目前所在的公司采用架构就是类似与Surging的RPC框架,在.NET 4.0框架 ...

  10. 看到一个对CAP简单的解释

    一个分布式系统里面,节点组成的网络本来应该是连通的.然而可能因为一些故障,使得有些节点之间不连通了,整个网络就分成了几块区域.数据就散布在了这些不连通的区域中.这就叫分区.当你一个数据项只在一个节点中 ...