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.

For example,

Given s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT",

Return:
["AAAAACCCCC", "CCCCCAAAAA"].

tag: two pointers

class Solution {
    public List<String> findRepeatedDnaSequences(String s) {
        List<String> ans = new ArrayList<>();
        if (s == null || s.length() < 10) return ans;
        Map<String, Integer> hash = new HashMap<>();
        int start = 0, end = 10;
        while (end <= s.length()){
            String str = s.substring(start, end);
            hash.put(str, hash.getOrDefault(str, 0) + 1);
            start++;
            end++;
        }

        for (String str : hash.keySet()){
            if (hash.get(str) > 1) ans.add(str);
        }

        return ans;
    }
}

results for ""

    No results matching ""