Idiot-maker

  :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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;
    }
}

 

posted on 2018-12-24 23:47  NickyYe  阅读(93)  评论(0编辑  收藏  举报