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:

Starting point is assumed to be valid, so it might not be included in the bank.
If multiple mutations are needed, all mutations during in the sequence must be valid.
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

the same with word ladder

写的时候语法上出了一些问题

第5行不用给char数组赋大小

第20行用stringbuilder的时候曾经写成:String afterMutation = new StringBuilder(cur).setCharAt(i, c).toString(); 这会有错因为.setCharAt()函数返回值是void;替代方法可以是char[] array = string.toCharArray(); string = new String(array);

 public class Solution {
public int minMutation(String start, String end, String[] bank) {
if (start==null || end==null || start.length()!=end.length()) return -1;
int steps = 0;
char[] mutations = new char[]{'A', 'C', 'G', 'T'};
HashSet<String> validGene = new HashSet<String>();
for (String str : bank) {
validGene.add(str);
}
if (!validGene.contains(end)) return -1;
if (validGene.contains(start)) validGene.remove(start);
Queue<String> q = new LinkedList<String>();
q.offer(start);
while (!q.isEmpty()) {
int size = q.size();
for (int k=0; k<size; k++) {
String cur = q.poll();
for (int i=0; i<cur.length(); i++) {
for (char c : mutations) {
StringBuilder ss = new StringBuilder(cur);
ss.setCharAt(i, c);
String afterMutation = ss.toString();
if (afterMutation.equals(end)) return steps+1;
if (validGene.contains(afterMutation)) {
validGene.remove(afterMutation);
q.offer(afterMutation);
}
}
}
}
steps++;
}
return -1;
}
}

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. Minimum Genetic Mutation 解题报告(Python & C++)

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

  3. 【leetcode】433. Minimum Genetic Mutation

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

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

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

  5. 1244. Minimum Genetic Mutation

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

  6. LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree

    LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...

  7. [LeetCode] Minimum Size Subarray Sum 解题思路

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  8. [LeetCode] Minimum Moves to Equal Array Elements II 最少移动次数使数组元素相等之二

    Given a non-empty integer array, find the minimum number of moves required to make all array element ...

  9. [LeetCode] Minimum Moves to Equal Array Elements 最少移动次数使数组元素相等

    Given a non-empty integer array of size n, find the minimum number of moves required to make all arr ...

随机推荐

  1. 【BZOJ】3670: [Noi2014]动物园

    http://www.lydsy.com/JudgeOnline/problem.php?id=3670 题意:太水了= = #include <bits/stdc++.h> using ...

  2. BZOJ4503: 两个串

    Description 兔子们在玩两个串的游戏.给定两个字符串S和T,兔子们想知道T在S中出现了几次, 分别在哪些位置出现.注意T中可能有“?”字符,这个字符可以匹配任何字符. Input 两行两个字 ...

  3. 《1Q84》--[日]村上春树

    <1Q84>,作者是:村上春树(村长) 故事梗概: 1984年,青豆与天吾皆为30岁,青豆为健身教练但另一面则是暗杀者,将受到极度暴力 的妇女们的丈夫们送至死亡的世界.天吾的职业为升大学的 ...

  4. 28个MongoDB 的问题

    MongoDB是目前最好的面向文档的免费开源NoSQL数据库.如果你正准备参加MongoDB NoSQL数据库的技术面试,你最好看看下面的MongoDB NoSQL面试问答.这些MongoDB NoS ...

  5. 油田 Oil Deposits

    油田 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=84562#problem/L 题意: 输入一个m行n列的字符矩形,统计字符 ...

  6. SQL中的with as

    一.WITH AS的含义 WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到.有的时候,是为了让 ...

  7. c# 函数练习

    1.out的使用 2.100以内与7无关的数(个位数是7,十位数是7,7的倍数) 每日一句:不求与人相比,但求超越自己,要哭就哭出激动的泪水,要笑就笑出成长的性格!

  8. Excel和datatable相互操作

    /// <summary> /// Excel文档 /// </summary> /// <param name="table"></pa ...

  9. MacPort 的使用

    MacPorts 的安装和使用   官网下载最版本的 安装包   自动安装  可能会出现很慢的情况   设置环境变量   vim ~/.bash_profile      i  插入修改  :wq 保 ...

  10. php 中如何创建一个空对象

    // 创建一个空对象 $obj=(object)array(); //假设这是从数据库取内容 $arr=["prod_id"=>103,"prod_name&quo ...