题目:

All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA.

Write a function to find all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule.

For example,

Given s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT",

Return:
["AAAAACCCCC", "CCCCCAAAAA"].

链接: http://leetcode.com/problems/repeated-dna-sequences/  

题解:

求repeating molecule of DNA sequence。最直接的想法就是从头遍历,把substring加入到HashMap中,然后进行比较。这样的话因为substring()的复杂度是O(n),所以整个算法复杂度是O(n2)。看到有讨论用Rabin-Karp的Rolling Hash自己做Hash Function。这样做的好处我觉得可能是减少了substring()的次数,但总得来说时间复杂度也还是O(n2)。而且做了Rabin-Karp的话要不要要不要判断collision,collision以后用Monte-carlo还是Las Vegas检测结果,也是问题。要再研究一下。

Time Complexity - O(n2), Space Complexity - O(n)。

public class Solution {
public List<String> findRepeatedDnaSequences(String s) {
List<String> res = new ArrayList<String>();
if(s == null || s.length() < 10)
return res;
Map<String, Integer> map = new HashMap<>(); for(int i = 0; i < s.length() - 9; i++) {
String subStr = s.substring(i, i + 10);
if(map.containsKey(subStr)) {
if(map.get(subStr) == 1)
res.add(subStr);
map.put(subStr, map.get(subStr) + 1);
} else
map.put(subStr, 1);
} return res;
}
}

二刷:

还是用的老方法,利用HashMap进行比较。

看到Stefan的用Set也能做,更像Python的风格,速度更快。

Rolling Hash的话,Time Complexity是O(n),但每次都要计算10个数的hash value,速度也不快。有机会的话联系一下好了。

Java:

HashMap:

Time Complexity - O(n2), Space Complexity - O(n)。

public class Solution {
public List<String> findRepeatedDnaSequences(String s) {
List<String> res = new ArrayList<>();
if (s == null) return res;
Map<String, Integer> map = new HashMap<>();
for (int i = 0; i + 10 <= s.length(); i++) {
String str = s.substring(i, i + 10);
if (!map.containsKey(str)) {
map.put(str, 1);
} else {
if (map.get(str) == 1) res.add(str);
map.put(str, 2);
}
}
return res;
}
}

Reference:

https://leetcode.com/discuss/24595/short-java-rolling-hash-solution

https://leetcode.com/discuss/24557/just-7-lines-of-code

https://leetcode.com/discuss/24478/i-did-it-in-10-lines-of-c

https://leetcode.com/discuss/25399/clean-java-solution-hashmap-bits-manipulation

https://leetcode.com/discuss/25536/am-understanding-the-problem-wrongly-what-about-aaaaccccca

https://leetcode.com/discuss/29623/11ms-solution-with-unified-hash-fxn

https://leetcode.com/discuss/54777/easy-to-understand-java-solution-with-well-commented-code

https://leetcode.com/discuss/46948/accepted-java-easy-to-understand-solution

https://leetcode.com/discuss/64841/7-lines-simple-java-o-n

187. Repeated DNA Sequences的更多相关文章

  1. [LeetCode] 187. Repeated DNA Sequences 求重复的DNA序列

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  2. leetcode 187. Repeated DNA Sequences 求重复的DNA串 ---------- java

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  3. Java for LeetCode 187 Repeated DNA Sequences

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  4. [LeetCode#187]Repeated DNA Sequences

    Problem: All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: ...

  5. [LeetCode] 187. Repeated DNA Sequences 解题思路

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  6. 【LeetCode】187. Repeated DNA Sequences

    题目: All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: " ...

  7. 187. Repeated DNA Sequences重复的DNA子串序列

    [抄题]: All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: &qu ...

  8. 187. Repeated DNA Sequences (String; Bit)

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  9. 187. Repeated DNA Sequences(建立词典,遍历一遍 o(n))

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

随机推荐

  1. 提升程序的特权(AdjustTokenPrivileges)

    首先列出需要的函数 1.OpenProcessToken 2.AdjustTokenPrivileges 3. LookupPrivilegeValue ----------------------- ...

  2. 安装WP8 SDK出现“根据当前系统时钟或签名文件中的时间戳验证时要求的证书不在有效期内”的解决办法

    今天重装系统了,在安装WP8 SDK时,安装了一小部分就提示“根据当前系统时钟或签名文件中的时间戳验证时要求的证书不在有效期内”的错误. 根据错误提示,貌似跟时间有关,百度了下.果真.把系统时间往前调 ...

  3. PHP 魔术方法 __clone __toString(五)

    __clone() - 当对象克隆的时候自动加载此方法 __toString() - 当对象需要echo打印输出的时候自动加载此方法 __clone() <?php class example{ ...

  4. GDB 进行调试 使用心得

    GDB 进行调试 使用心得 转 1: 对于在应用程序中加入参数进行调试的方法:   直接用 gdb app -p1 -p2 这样进行调试是不行的.   需要像以下这样使用:    #gdb app   ...

  5. 关于html的下载功能

    新项目基本告一段落,第一次完成前后端分离的集成,遇到的坑自然不少. 来说说第一天遇到的其中一个坑吧. ——关于下载的问题... 以前的做法,大家都喜爱用<a></a>标签吧.而 ...

  6. jQuery(function($){...})与(function($){...})(jQuery)知识点分享

    写jQuery插件时一些经验分享一下. 一.推荐写法 jQuery(function($){ //coding }); 全写为 jQuery(document).ready(function($){ ...

  7. Android源代码编译——下载

    下了好久的源代码,真真是慢哈.真希望国内有公司能够把镜像开放出来. 不多说,首先是系统环境,我的系统是Ubuntu 64位系统(14.04), 版本应该没什么. 需要的库 Git: 没话说必须, su ...

  8. sublime 设置localhost 2

    最近sidebar用不了了,提示更新然后就自动卸载了: 研究了下其他方式实现: Sublime Text 2 Sublime Text 3 都可以使用: 菜单 --> Tools --> ...

  9. mac 下 sphinx + mysql + php 实现全文搜索(xampp)(1)

      原理: 使用sphinx 中的indexer 生成索引数据 service/web 端 利用searched 调用索引数据 步骤: 下载 sphinx: 下载地址:http://sphinxsea ...

  10. hadoop可能遇到的问题

    1.hadoop运行的原理? 2.mapreduce的原理? 3.HDFS存储的机制? 4.举一个简单的例子说明mapreduce是怎么来运行的 ? 5.面试的人给你出一些问题,让你用mapreduc ...