-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepeatedDNASequences.java
More file actions
57 lines (48 loc) · 1.37 KB
/
RepeatedDNASequences.java
File metadata and controls
57 lines (48 loc) · 1.37 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
50
51
52
53
54
55
56
57
public class RepeatedDNASequences {
public List<String> findRepeatedDnaSequences(String s) {
List<String> ret = new LinkedList<String>();
if(s==null||s.length()<10) return ret;
HashSet<Integer> hs = new HashSet<Integer>();
boolean[] hashRec = new boolean[1<<20];
int temp = 0;
for(int i=0;i<9;i++){
char c=s.charAt(i);
temp<<=2;
temp = temp|convert(c);
temp = temp & ((1<<20)-1);
}
for(int i=9;i<s.length();i++) {
char c=s.charAt(i);
temp<<=2;
temp = temp|convert(c);
temp = temp & ((1<<20)-1);
if(hashRec[temp]){
if(!hs.contains(temp)){
hs.add(temp);
ret.add(s.substring(i-9,i+1));
}
} else {
hashRec[temp] = true;
}
}
return ret;
}
private int convert(char c) {
int i=0;
switch(c) {
case 'A':
i=0;
break;
case 'C':
i=1;
break;
case 'G':
i=2;
break;
case 'T':
i=3;
break;
}
return i;
}
}