题目:
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA.
Write a function to find all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule.
For example,
Given s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT", Return: ["AAAAACCCCC", "CCCCCAAAAA"].
分析:
利用hash表。
代码:
class Solution {
public:
vector<string> findRepeatedDnaSequences(string s) {
int hashmap[1048576]={},hashnum=0;
vector<string> res;
int len = s.length();
for(int i=0;i<9;++i){
hashnum=hashnum<<2|(s[i]-'A'+1)%5;
}
for(int i=9;i<len;++i){
if(hashmap[hashnum=(hashnum<<2|(s[i]-'A'+1)%5)&0xfffff]++==1)
res.push_back(s.substr(i-9,10));
}
return res;
}
};