Leetcode 187: Repeated DNA Sequences
Total Accepted:1161Total Submissions:6887
All DNA is composed of a series of nucleus otides 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 = "aaaaacccccaaaaaccccaaaaagggttt", Return: ["AAAAACCCCC", "CCCCCAAAAA"].
[Analysis]
The HASHMAP method exceed space limit.
Because there are only four letters, you can create your own hashkey. Each two BITS corresponds to an incoming character. If the value exceeds 20 BITS, that is, 10 characters are retained, only 20 BITS are retained.
[Note]
1. (hash <2) + map. get (c) symbol priority, <must be included.
public class Solution { public List
findRepeatedDnaSequences(String s) { List
res = new ArrayList
(); if(s==null || s.length() < 11) return res; int hash = 0; Map
map = new HashMap
(); map.put('A', 0); map.put('C', 1); map.put('G', 2); map.put('T', 3); Set
set = new HashSet
(); Set
unique = new HashSet
(); for(int i=0; i