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.

Example:

Input: s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"

Output: ["AAAAACCCCC", "CCCCCAAAAA"]

Solution: count the frequency of 10 letter words

class Solution {
//find all the 10-letter-long sequences that occur more than once in a DNA molecule
public List<String> findRepeatedDnaSequences(String s) {
//substring -- subset n +n-1+...+1: n-k+1
List<String> res = new ArrayList<String>();
Map<String,Integer> map = new HashMap<String,Integer>();
int n = s.length();
int k =10;
if(n < k) return res;
for(int i = 0; i<=n-k; i++){//11-10 1
String sub = s.substring(i, i+k);
if(map.containsKey(sub)){
map.put(sub, map.get(sub)+1);
}else {
map.put(sub, 1);
}
}
for(Map.Entry<String, Integer> entry : map.entrySet()){
if(entry.getValue() >1){
res.add(entry.getKey());
}
}
return res;
}
}

Solution 2: two HashSet with a non-duplicate feature.

public List<String> findRepeatedDnaSequences(String s) {
Set seen = new HashSet(), repeated = new HashSet();
for (int i = 0; i + 9 < s.length(); i++) {
String ten = s.substring(i, i + 10);
if (!seen.add(ten))//if add then first time, else add it
repeated.add(ten);
}
return new ArrayList(repeated);
}

subsequence & substring

subsequence: subset 2^n

substring: continous string : n+n-1+n-2+...+1

*187. Repeated DNA Sequences (hashmap, one for loop)(difference between subsequence & substring)的更多相关文章

  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. 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 ...

  3. 187. Repeated DNA Sequences

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

  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: " ...

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

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

  7. 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 ...

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

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

  9. 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 ...

随机推荐

  1. HDU1398 Square Coins

    Description People in Silverland use square coins. Not only they have square shapes but also their v ...

  2. django 视图view

    视图里一般是函数和类,需要返回响应. 试图分为2种:2. CBV(class base view) FBV(function base view) from django.views import V ...

  3. python3 关键字和可变参数笔记

    """普及一下字典的知识""" # dict = {'Name': 'Runoob', 'Age': 7, 'Class': 'First' ...

  4. zookeeper崩溃修复

    cat /etc/zookeeper/conf/zoo.cfg 找到dataDir=/var/lib/zookeeper 切换到路径/var/lib/zookeeper cd /var/lib/zoo ...

  5. Linux 系统 文件锁 fcntl函数详解

    #include <unistd.h> #include <fcntl.h> int fcntl(int fd, int cmd); int fcntl(int fd, int ...

  6. 数据库版本管理工具flyway

    引入flyway_core  jar包 java 代码实现 public class FlywayMigration { @Resource private DataSource dataSource ...

  7. 时间比较早晚java

    package demo; import java.text.SimpleDateFormat;import java.util.Date;import java.util.Locale; publi ...

  8. vue学习中遇到的onchange、push、splice、forEach方法使用

    最近在做vue的练习,发现有些js中的基础知识掌握的不牢,记录一下: 1.onchange事件:是在域的内容改变时发生,单选框与复选框改变后触发的事件. 2.push方法:向数组的末尾添加一个或多个元 ...

  9. Sql 本周当天本期日期转换

    --查询当天: --查询24小时内的: --info为表名,datetime为数据库中的字段值 --查询当天: --查询24小时内的: select * from table where DateDi ...

  10. Hibernate课程 初探多对多映射1-1 多对多应用场景

    1 用途: 员工和项目之间的多对多关系 2 实现: 员工表和项目表之外,建立员工和项目关联表来实现: 3 hibernate应用: set元素和many-to-many来实现