# LeetCode Repeated DNA Sequences

Description:

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

Solution:

import java.util.*;

public class Solution {
public List<String> findRepeatedDnaSequences(String s) {
List<String> list = new ArrayList<String>();
TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>();

if (s.length() < 10)
return list;

int temp = 0, num;
for (int i = 0; i < 9; i++) {
temp = temp << 2 | convert(s.charAt(i));
}
for (int i = 9; i < s.length(); i++) {
temp = (temp << 2 | convert(s.charAt(i))) & 0xFFFFF;
if (map.containsKey(temp)) {
num = map.get(temp);
map.put(temp, num + 1);
} else
map.put(temp, 1);
}

String neo;
Iterator<Integer> ite = map.keySet().iterator();
while (ite.hasNext()) {
temp = ite.next();
num = map.get(temp);
if (num == 1)
continue;
neo = "";
for (int i = 0; i < 10; i++) {
neo = (char) convert(temp % 4) + neo;
temp >>= 2;
}
}

return list;
}

int convert(int ch) {
switch (ch) {
case 'A':
return 0;
case 'C':
return 1;
case 'G':
return 2;
case 'T':
return 3;
case 0:
return 'A';
case 1:
return 'C';
case 2:
return 'G';
case 3:
return 'T';
}
return 0;
}
}

• 本文已收录于以下专栏：

## 【LeetCode】Repeated DNA Sequences 解题报告

【题目】 All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "A...
• ljiabin
• 2015年03月20日 11:19
• 5609

## [leetcode-187]Repeated DNA Sequences(java)

• zdavb
• 2015年08月28日 22:20
• 630

## leetcode 187: Repeated DNA Sequences

Total Accepted: 1161 Total Submissions: 6887 All DNA is composed of a series of nucleotides abb...
• xudli
• 2015年02月09日 07:29
• 4652

## 187. Repeated DNA Sequences Leetcode Python

All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTC...
• hyperbolechi
• 2015年03月16日 11:21
• 1097

## LeetCode187——Repeated DNA Sequences

All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTC...
• booirror
• 2015年08月18日 23:47
• 1021

## leetcode 187. Repeated DNA Sequences 编码计数统计重复字符串 + 移动窗口

All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: “ACGAATTC...
• JackZhang_123
• 2017年09月20日 09:44
• 272

## Leetcode 187 Repeated DNA Sequences 重复出现的DNA序列

• smile_watermelon
• 2015年08月05日 15:54
• 310

## leetcode187. Repeated DNA Sequences

All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: “ACGAATTC...
• tinkle181129
• 2016年01月26日 15:12
• 166

## [LeetCode]Repeated DNA Sequences，解题报告

• zinss26914
• 2015年03月14日 12:09
• 3459

## leetcode_c++：哈希： Repeated DNA Sequences(187)

All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: “ACGAATTC...
• mijian1207mijian
• 2016年07月09日 14:45
• 195

举报原因： 您举报文章：LeetCode Repeated DNA Sequences 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)