-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepeatedDNASequences.java
More file actions
50 lines (48 loc) · 1.79 KB
/
Copy pathRepeatedDNASequences.java
File metadata and controls
50 lines (48 loc) · 1.79 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
// optimization explanation: https://leetcode.com/discuss/74330/20-ms-solution-c-with-explanation
// 1. encode string(length 10) as integer(20 bits)
// a. string comparison is expensive
// b. make use of overlapping
// 2. use BitSet instead of HashSet
// a. reduce Value memory: integer -> bit
// b. reduce Key memory: do not need to store key, use index
import java.util.BitSet;
import java.util.ArrayList;
import java.util.List;
public class RepeatedDNASequences{
public List<String> findRepeatedDnaSequences(String s) {
ArrayList<String> result = new ArrayList<String>();
if(s.length() > 10){
int[] charToInt = new int[26];
charToInt['A' - 'A'] = 0;
charToInt['C' - 'A'] = 1;
charToInt['G' - 'A'] = 2;
charToInt['T' - 'A'] = 3;
BitSet s1 = new BitSet(1<<20);
BitSet s2 = new BitSet(1<<20);
int code = 0;
int mask = (1 << 20) - 1;
for(int i = 0; i < 10; i++){
code = (code << 2) | charToInt[s.charAt(i) - 'A'];
}
s1.set(code);
for(int i = 10; i < s.length(); i++){
code = ((code << 2) & mask) | charToInt[s.charAt(i) - 'A'];
if(!s2.get(code)){
if(s1.get(code)){
result.add(s.substring(i-9, i+1));
s2.set(code);
}
else
s1.set(code);
}
}
}
return result;
}
public static void main(String[] argvs){
RepeatedDNASequences rds = new RepeatedDNASequences();
for(String s: rds.findRepeatedDnaSequences("AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT")){
System.out.println(s);
}
}
}