题目:

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. Windows 右键添加「cmd 打开」

    1. 2. 3. 参考: 1.Windows右键添加"使用CMD打开" 2.WIN7.WIN8 右键在目录当前打开命令行Cmd窗口(图文)

  2. Contest1065 - 第四届“图灵杯”NEUQ-ACM程序设计竞赛(个人赛)E粉丝与分割平面

    题目描述 在一个平面上使用一条直线最多可以将一个平面分割成两个平面,而使用两条直线最多可将平面分割成四份,使用三条直线可将平面分割成七份--这是个经典的平面分割问题,但是too simple,作为一个 ...

  3. xposed系列教程

    转载说明 本篇文章可能已经更新,最新文章请转:http://www.sollyu.com/xposed-series/ 说明 这是一篇XPOSED开发系列教程, 个人开发者,开发不容易.QQ群: 73 ...

  4. [转]Nuget挂了的解决方法

    今天用Nuget下一个程序包时,发现Nuget挂了:未能解析此远程名称:'nuget.org'.第一反应就是方校长抖威风了,挂个代理上 http://nuget.org 试了下,果然好好的. 用命令n ...

  5. QT设置窗口屏幕居中

    int main(int argc, char *argv[]){  QApplication ap(argc, argv);  QDesktopWidget *pDesk = QApplicatio ...

  6. Check Big/Little Endian

    Little endian:Low memory address stores low byte value.(eg.  short int 0x2211   0xbfd05c0e->0x11 ...

  7. DTCMS更改图片相册上传图片类型,手机上传图片相册

    /admin/js/uploader.js 中 filetypes: "jpg,jpge,png,gif", //文件类型 改为 filetypes: "jpg,jpeg ...

  8. 【原】在一般处理程序中设置session

    using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using Syste ...

  9. 使用SQLyog远程访问mysql数据库设置

    mysql数据库远程访问设置方法 1.修改localhost更改 "mysql" 数据库里的 "user" 表里的 "host" 项,从&q ...

  10. PHPCMS 错误日志 Only variables should be passed by ...

    有几个网站是PHPCMS V9做的,但这两天发现一个问题,PHPCMS 的错误日志超过了20M ,后台报警,然后我看了下错误日志,其中两万多行都是一个错误,错误信息如下: 1 <?php exi ...