码迷,mamicode.com
首页 > 其他好文 > 详细

Repeated DNA Sequences

时间:2016-07-31 14:26:24      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

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"].
 1 public class Solution {
 2     public List<String> findRepeatedDnaSequences(String s) {
 3         List<String> list = new ArrayList<String>();
 4         Set<String> temp = new HashSet<String>();
 5         if (s == null || s.length() <= 10)
 6             return list;
 7         Set<String> set = new HashSet<String>();
 8         for (int i = 10; i <= s.length(); i++) {
 9             if (set.contains(s.substring(i - 10, i))) {
10                 temp.add(s.substring(i - 10, i));
11             } else {
12                 set.add(s.substring(i - 10, i));
13             }
14         }
15         return new ArrayList<String>(temp);
16     }
17 }

 

Repeated DNA Sequences

标签:

原文地址:http://www.cnblogs.com/beiyeqingteng/p/5722813.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!