-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepeatDna.java
More file actions
49 lines (46 loc) · 1.19 KB
/
repeatDna.java
File metadata and controls
49 lines (46 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//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.
public class Solution {
public List<String> findRepeatedDnaSequences(String s) {
List<String> result=new ArrayList<String>();
HashMap<Integer, Integer> map = new HashMap<Integer,Integer>();
for(int i = 0; i <= s.length() - 10 ; i++) {
String sub = s.substring(i, i+10);
int newKey = getKey(sub);
if(map.containskey(newKey)){
map.put(newKey,map.get(newKey)+1);
if(map.get(newKey) == 2){
result.add(sub);
}
}
else
map.put(key,1);
}
return result;
}
public int gerKey(String S){
int result = 0;
for(int i = s.length()-1; i >= 0; i--) {
int match = 0;
switch(s.charAt(i)) {
case 'A':
match |= 0;
break;
case 'T':
match |= 1;
break;
case 'G':
match |= 2;
break;
case 'C':
match |= 3;
break;
}
result = match | result;
result = result << 2;
}
return result;
}
}