-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathsolution.cpp
45 lines (44 loc) · 1.02 KB
/
solution.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
43
44
45
class Solution
{
public:
int getVal(char ch)
{
if (ch == 'A') return 0;
if (ch == 'C') return 1;
if (ch == 'G') return 2;
if (ch == 'T') return 3;
return 0;
}
vector<string> findRepeatedDnaSequences(string s)
{
set<string> st;
vector<string> res;
string str;
if (s.length() < 10 || s == "")
return res;
int mp[1024*1024] = {0};
unsigned int val = 0;
for (int i = 0; i < 9; ++i)
{
val <<= 2;
val |= getVal(s[i]);
}
for (int i = 9; i < s.length(); ++i)
{
val <<= 14;
val >>= 12;
val |= getVal(s[i]);
++mp[val];
if (mp[val] > 1)
{
str = s.substr(i-9, 10);
st.insert(str);
}
}
for (set<string>::iterator i = st.begin(); i != st.end(); ++i)
{
res.push_back(*i);
}
return res;
}
};