1. Title
Repeated DNA Sequences
2. Http address
https://leetcode.com/problems/repeated-dna-sequences/
3. The question
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"].
4. My code (AC)
1 //Accept 2 public static List<String> findRepeatedDNASeq(String s){ 3 4 List<String> re = new ArrayList<String>(); 5 Map<String,Integer> dit = new HashMap<String,Integer>(); 6 String key = ""; 7 int value = 0; 8 if ( s == null || s.length() < 10 ) 9 { 10 return re; 11 } 12 13 for(int i = 0 ; i <= s.length() - 10;i++ ) 14 { 15 key = s.substring(i,i+10); 16 if( dit.containsKey(key) ) 17 { 18 // i += 10; 19 value = dit.get(key); 20 if( value == 1 ) 21 { 22 re.add(key); 23 dit.put(key,0); 24 } 25 26 }else{ 27 dit.put(key,1); 28 } 29 } 30 31 return re; 32 }