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

187. Repeated DNA Sequences

时间:2018-12-25 00:07:03      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:tor   sub   enc   com   nas   input   sequence   class   ted   

https://leetcode.com/problems/repeated-dna-sequences/

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

解题思路:

需要注意的是,因为substring的长度是const的10,所以实际上是O(10n)。

class Solution {
    public List<String> findRepeatedDnaSequences(String s) {
        List<String> res = new ArrayList<String>();
        
        if (s.length() < 11) {
            return res;
        }
        
        StringBuffer sb = new StringBuffer();
        Map<String, Integer> map = new HashMap<String, Integer>();        
        int index = 0;
        
        while (index <= s.length() - 10) {
            String temp = s.substring(index, index + 10);
            int count = map.getOrDefault(temp, 0);
            if (count == 1) {
                res.add(temp);
                map.put(temp, count + 1);
            } else if (count == 0){
                map.put(temp, 1);
            }
            index++;
        }
        
        return res;
    }
}

 

187. Repeated DNA Sequences

标签:tor   sub   enc   com   nas   input   sequence   class   ted   

原文地址:https://www.cnblogs.com/NickyYe/p/10171725.html

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