-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepeatedDNASequences.cpp
More file actions
executable file
·62 lines (52 loc) · 1.43 KB
/
RepeatedDNASequences.cpp
File metadata and controls
executable file
·62 lines (52 loc) · 1.43 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
58
59
60
61
62
//
// RepeatedDNASequences.cpp
// leetcode
//
// Created by witwolf on 3/30/15.
// Copyright (c) 2015 witwolf. All rights reserved.
//
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Solution {
private:
int four(char c){
static int chars['T' - 'A' + 1] = {
0x0,0,0b1,0,0,
0,0b10,0,0,0,
0,0,0,0,0,
0,0,0,0,0b11
};
return chars[c - 'A'];
}
public:
vector<string> findRepeatedDnaSequences(string s) {
vector<string> result;
int counter[1048576] = {0};
int start = 0,end = 0;
int num = 0 ;
for(;end < s.length();end++){
num <<= 2;
num &= 0xfffff;
num |= four(s[end]);
if(end - start == 9){
if(counter[num] == 1){
result.push_back(s.substr(start,10));
}
counter[num] += 1;
start ++ ;
}
}
return result;
}
};
int main(int argc,char **argv){
Solution s;
string s1("AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT");
vector<string> result = s.findRepeatedDnaSequences(s1);
copy(result.begin(),result.end(),ostream_iterator<string>(cout, " "));
string s2("AAAAAAAAAAAAA");
result = s.findRepeatedDnaSequences(s2);
copy(result.begin(),result.end(),ostream_iterator<string>(cout, " "));
}