Subsequence - 187. Repeated DNA Sequences

2020-09-23 17:16:07 浏览数 (4)

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:

代码语言:javascript复制
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);
    }
}

1 人点赞