Subsequence - 187. Repeated DNA Sequences

时间:2022-07-25
本文章向大家介绍Subsequence - 187. Repeated DNA Sequences,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

187. 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.

思路:

题目意思是找出重复的序列,采用滑动窗口和hashset不可以重复的特性来做。

代码:

java:

class Solution {

    public List<String> findRepeatedDnaSequences(String s) {
        Set curr = new HashSet(), repeated = new HashSet();
        for (int i = 0; i + 9 < s.length(); i++) {
            String ten = s.substring(i, i + 10);
            if (!curr.add(ten))
                repeated.add(ten);
        }
        return new ArrayList(repeated);
    }
}