187. Repeated DNA Sequences

Posted nickyye

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了187. Repeated DNA Sequences相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

187 Repeated DNA Sequences 重复的DNA序列

187. Repeated DNA Sequences

187. Repeated DNA Sequences

187. Repeated DNA Sequences

187. Repeated DNA Sequences

[LeetCode] 187. Repeated DNA Sequences 求重复的DNA序列