187. Repeated DNA Sequences
problem description
Input: s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
Output: ["AAAAACCCCC", "CCCCCAAAAA"]algorithm thought
code
class Solution {
public:
vector<string> findRepeatedDnaSequences(string s) {
unordered_map<char,int> ma{{'A',0},{'C',1},{'G',2},{'T',3}};
unordered_map<int,int> cache;
vector<string>res;
int hash=0,mask=(1<<20)-1;
for(int i=0;i<s.size();++i){
hash=(hash<<2)+ma[s[i]];
if(i>=9){
hash=hash&mask;
cache[hash]++;
if(cache[hash]==2){
res.push_back(s.substr(i-9,10));
}
}
}
return res;
}
};algorithm analysis
Last updated