-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathRepeatedDNASequences.java
More file actions
28 lines (25 loc) · 986 Bytes
/
RepeatedDNASequences.java
File metadata and controls
28 lines (25 loc) · 986 Bytes
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
import java.util.*;
public class RepeatedDNASequences {
public List<String> findRepeatedDnaSequences(String s) {
List<String> res = new ArrayList<>();
if (s.length() < 11) return res;
Map<Integer, Integer> map = new HashMap<>();
Map<Character, Integer> charMap = new HashMap<>();
charMap.put('A', 0);
charMap.put('C', 1);
charMap.put('G', 2);
charMap.put('T', 3);
int substrInt = 0;
for (int i = 0; i < s.length(); i++) {
if (i < 9) {
substrInt = (substrInt << 2) + charMap.get(s.charAt(i));
} else {
substrInt = (substrInt << 2) + charMap.get(s.charAt(i));
map.put(substrInt, (map.containsKey(substrInt) ? map.get(substrInt) : 0) + 1);
if (map.get(substrInt) == 2) res.add(s.substring(i - 9, i + 1));
substrInt = substrInt & ((1 << 18) - 1);
}
}
return res;
}
}