-
Notifications
You must be signed in to change notification settings - Fork 0
/
187_Repeated-DNA-Sequences.cpp
42 lines (39 loc) · 1.16 KB
/
187_Repeated-DNA-Sequences.cpp
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
class Solution {
private:
int encode_DNA(char c){
return (c == 'A') ? 0 : (c == 'C') ? 1 : (c == 'G') ? 2 : 3;
}
string decode_DNA(int val) {
string output_str(10, ' ');
for(int i = 9; i >= 0; --i) {
output_str[i] = (val & 3) == 0 ? 'A' : (val & 3) == 1 ? 'C' : (val & 3) == 2 ? 'G' : 'T';
val >>= 2;
}
return output_str;
}
public:
vector<string> findRepeatedDnaSequences(string s) {
unordered_set<int> output;
unordered_set<int> seen;
int n = s.length();
if(n < 10) return vector<string>();
//A: 00, C:01, G:10, T:11
int curr = 0;
int mask = (1 << 20) - 1; // 20-bit mask for storing the encoded value
for(int i = 0; i < n; ++i){
curr = (curr << 2 | encode_DNA(s[i])) & mask;
if(i >= 9){
if(seen.count(curr)){
output.insert(curr);
}else{
seen.insert(curr);
}
}
}
vector<string> ans;
for(int val : output){
ans.push_back(decode_DNA(val));
}
return ans;
}
};