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;
}
}

最新文章

  1. web前端学习笔记(CSS变化宽度布局)
  2. ASP.NET + SqlSever 大数据解决方案 PK HADOOP
  3. tomcat错误信息解决方案【严重:StandardServer.await: create[8005]
  4. LeeCode-Rotate Array
  5. Xcode如何添加字体库--
  6. 读书笔记:《为什么大猩猩比专家高明, How We Decide》
  7. linux 内核的rt_mutex (realtime互斥体)
  8. 在Codeblocks下配置GoogleTest单元测试工具
  9. 【java设计模式】(3)---代理模式(案例解析)
  10. 第一次使用VS Code时你应该知道的一切配置
  11. centos 7 免密登录
  12. IE9 添加事件DOMContentLoaded,方法addEventListener
  13. Haskell语言学习笔记(20)IORef, STRef
  14. Centos记录所有用户登录和操作的详细日志
  15. AutoFac使用方法总结四:生命周期续
  16. &lt;Android 基础(二十六)&gt; 渐变色圆角Button
  17. [WPF]解决模板中ContextMenu绑定CommandParameter的问题
  18. 初识asp
  19. Qt-QML-电子罗盘
  20. 160726 smarty 笔记(2)

热门文章

  1. IE6下div层被select控件遮住的问题解决方法
  2. List&lt;string&gt;中的泛型委托
  3. Deep Copy cv::StereoBM 深度拷贝
  4. [LintCode] Intersection of Two Arrays 两个数组相交
  5. Maya 2015 中英文切换
  6. Linux_用户/用户组
  7. KindEditor用法介绍
  8. width,clientWidth,offsetWidth
  9. asp.net自定义404页面
  10. 关于WebDAV带来的网站潜在安全问题的疑问